Computer Science, asked by criazhoran6675, 7 months ago

Company Tesla has kept a contest for catchy caption for the image. They have a rule that the caption should not be more than 10 words. If the caption of a candidate is more than 10 words, then the candidature stands to be disqualified. Can you write a program to check whether a caption is eligible for the contest or not.
The maximum sizeof the caption is of 100 bytes.

Answers

Answered by LivviHelps
0

Step 1 - what's needed?

First, let's identify what needs to be included in the code.

1. Assuming that the user put a space in between each word, there will be 9 spaces in the caption. If there are more than 9 spaces, that would mean there are more than 10 words, therefore the caption is ineligible. This is one way you can check the number of words.

2. Do you need a text input? Or is the program simply checking a string already inputted into a variable?

So now you know what needs to be included, let's write some pseudo code.

Step 2 - pseudo code

You haven't stated what language you're using, so we'll go with standard python phrasing.

caption = input('Enter your slogan. Make sure it is no longer than 10 words.')

This puts the user input in response to the prompt ('Enter your slogan...') into a variable named 'caption'.

numSpaces = caption.count(' ') This counts the number of spaces in the variable 'caption' and puts that number into the variable 'numSpaces'.

if numSpaces > 9 : This checks if there are more than 9 spaces in the caption.

(indent here) print('There are more than 10 words in your caption.)

else : ie if there are 9 or less spaces, do the following:

(indent here) print('There are 10 or less words in your caption. It has been accepted.')

Final notes

Of course, these are just ideas, and naturally everything following the if statement is changeable, as is the rest. I hope this helps!

Answered by atharvakpol
3

Answer:

#include <cstring>

#include <iostream>

using namespace std;

int main()

{

  char s[100];

 cin.getline(s,100);

 int len = strlen(s);

 int word=0;

 for(int i=0;i<len;i++)

 {

 if(s[i]==' ')

   word++;

 }

 if(word<=10)

 cout<<"Caption eligible for the contest"<<endl;

 else  

   cout<<"Caption not eligible for the contest"<<endl;

 }

Explanation: Check for spaces between the words in the string . If we encounter spaces then increment word by 1 . if spaces (i.e. count of words) is more than 10 , then  caption is not eligible.

Similar questions