How to write a c program to word count in c programming ?
Solution:
#include <stdio.h>
#define SIZE 100
#define IN 1
#define OUT 2
int word_count(char str[]);
int main()
{
char str[SIZE];
gets(str);
printf("%d\n", word_count(str));
return 0;
}
int word_count(char str[])
{
int i, state = OUT, count = 0;
for(i = 0; str[i] != '\0'; i++)
{
if((str[i] >= 'a' && str[i] <= 'z' ) || (str[i] >= 'A' && str[i] <= 'Z'))
{
if(state == OUT)
{
state = IN;
count++;
}
}
else
state = OUT;
}
return count;
}