C program to count number of word in string using unlimited while loop.
C program to count number of word in string using unlimited while loop.
#include <stdio.h>
#define MAX 100
int main()
{
char text[MAX];
char prev;
int i, words;
printf("\n Enter any string: ");
gets(text);
i = 0;
words = 0;
prev = '\0';
while(1)
{
if(text[i]==' ' || text[i]=='\n' || text[i]=='\t' || text[i]=='\0')
{
if(prev != ' ' && prev != '\n' && prev != '\t' && prev != '\0')
{
words++;
}
}
prev = text[i];
if(text[i] == '\0')
break;
else
i++;
}
printf("\n Number of words in the enter string = %d", words);
return 0;
}
Output:
Enter any string: C program to count number of word in string
Number of words in the enter string = 9
Write a C program to count total number of words in a string.