Program to count number of words in a Line in C Source Code.
Language : C
//count nos. of words
#include
#include
void main()
{ char str[50];
int i,count,countc;
printf(“Enter a string : “);
gets(str);
count=0;
i=0;
while(str[i]!=’\0′)
{ if(str[i]==’ ‘)
count++;
i++;
}
printf(“The total number of words are %d “,count+1);
}
[/sourcecode]
Related posts :







{ 4 comments… read them below or add one }
I tried this phrase “aeiou”, and appeared as “The total of the 5 vowels consonants 4″, but has NO consonants O.O
And if i want to count whitespace, how to do that?
Thanks
@Felipe
I have updated the code to count the number of whitespaces to. The code at this post counts the words. For the source code of Vowels & Consonants +white-spaces please visit the following post : Count number of Vowels and Consonants in a String
I tried this phrase “aeiou”, and appeared as “The total of the 5 vowels consonants 4″, but has NO consonants O.O
And if i want to count whitespace, how to do that?
Thanks
@Felipe
I have updated the code to count the number of whitespaces to. The code at this post counts the words. For the source code of Vowels & Consonants +white-spaces please visit the following post : Count number of Vowels and Consonants in a String