C Program – Count the number of words in a given string

by Nideesh C on April 18, 2011 · 0 comments

in C Programming




#include<stdio.h>
#include<conio.h>
void main()
{
char str[50];
int words = 0, len = 0, i;
clrscr();
printf(“nnt ENTER A STRING…: “);
gets(str);
while(str[len]!=’′)
len++;
len–;
for(i=0;i<=len;i++)
{
if ( ( str[i] == ‘ ‘ && str[i+1] != ‘ ‘ ) || i == len )
words++;
}
printf(“nt NUMBER OF WORDS IN THE ABOVE SENTENCE IS…: %d”, words);
getch();
}

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C Program – Count the number of vowels in a given string
  2. C Program – Find the total number of palindrome in a given string
  3. C program – To check whether the given string is palindrome
  4. Sample – Program to count the number of words and characters in a sentence.
  5. C program to accept a string and find the number of times the word ‘the’ appears in it

Leave a Comment

Previous post:

Next post: