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.
[Hint:Use functions defined in cstring]
Answers
Answer:
#include <stdio.h>
#include <string.h>
int main()
{
char s[200];
int count = 0, i;
scanf("%[^\n]s", s);
for (i = 0;s[i] != '\0';i++)
{
if (s[i] == ' ' && s[i+1] != ' ')
count++;
}
if((count+1)<10)
printf("Caption eligible for the contest");
else
printf("Caption not eligible for the contest");
}
Explanation:
Answer:
//Using char array
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char str[100];
int len,count=0;
cin.getline(str,100);
len=strlen(str);
for(int i=0;i<strlen(str);i++)
if(str[i]==' ')
count++;
if(count+1>10)
cout<<"Caption not eligible for the contest";
else
cout<<"Caption eligible for the contest";
}
/*
// Using string class
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
string str;
int len,count=0;
getline(cin, str);
for(int i=0;i<str.length();i++)
if(str[i]==' ')
count++;
if(count+1 >10)
cout<<"Caption not eligible for the contest";
else
cout<<"Caption eligible for the contest";
}*/
Explanation: