Program to count number of words from a given sentence
The below mentioned C Program will count number of words from a given sentence. The program will accept a sentence in a character array sen[100]. It will then calculate the number of words and display the result.
#include#include #include void main() { char sen[100]; int i, count = 0; printf("Enter a Sentence : \n"); gets(sen); for (i=0; i<strlen(sen);i++) { if (isspace(sen[i])) { count++; } } printf("Number of words in the sentence = %d", ++count); printf("\n"); getch(); }
Also See:
Strings in C Programming Language
Doesn’t work there is a syntax error stating that it is missing ‘;’ before ‘)on line 12.
for (i=0;i and count=1
Thanks…
u hve entr a erong entry… that is getch()
It work’s
what if there is ‘,'(comma) symbol in the given sentence??
this does not work for sentence having multiple spaces between two words
very good work , although there are some mistake .
I correct it and here is the new code :
#include
#include
using namespace std;
int main()
{
char sen[100];
int i, count = 0;
printf(“Enter a Sentence : \n”);
gets(sen);
for (i=0; i {
if (isspace(sen[i]))
{
count++;
}
}
printf(“Number of words in the sentence = %d”, ++count);
printf(“\n”);
system(“pause”);
}
can anyone do the same code using pointers??
what if I give the input as –
Hello I am there.
ur ouput will be wrong..
what if there are 3 spaces before each word ???? each time it wil count the spaces as words ryt ???? how is it possible?