Count number of Words in a String

C Programming

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]



{ 4 comments… read them below or add one }

1 Felipe October 23, 2009 at 8:05 pm

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

Reply

2 Programming Kid October 27, 2009 at 3:48 am

@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

Reply

3 Felipe October 23, 2009 at 10:05 pm

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

Reply

4 Programming Kid October 27, 2009 at 5:48 am

@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

Reply

Leave a Comment

Notify me of followup comments via e-mail. You can also subscribe without commenting.


WordPress - Vaibhav Kanwal