Count the number of words in a given string using C


Levels of difficulty: / perform operation:

C a program to count the number of words in a given string. Two words are separated by one or more blank spaces.

C Program

#include <stdio.h>
#include <conio.h>
void main()
{
	char a[100];
	int len,i,word=1;
	clrscr();
	printf("\nENTER A STRING: ");
	gets(a);
	len=strlen(a);
	for(i=0;i<len;i++)
	{
		if(a[i]!=' ' && a[i+1]==' ')
			word=word+1;
	}
	printf("\nTHERE ARE %d WORDS IN THE STRING",word);
	getch();
}


Output