C program to count number of word in string using while loop.
C program to count number of word in string using while loop.
#include <stdio.h>
#define MAX 100
int main()
{
char text[MAX];
int i, words;
printf("\n Enter any string: ");
gets(text);
i = 0;
words = 1;
while(text[i] != '\0')
{
if(text[i]==' ' || text[i]=='\n' || text[i]=='\t')
{
words++;
}
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.