include stdio include stdlib int main FILE fp char buf int word_count

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *fp;
char buf = ' ';
int word_count=0, flag=0;
if((fp = fopen("text.txt", "r")) == NULL)
printf("Error to open file");
while(fread(&buf, sizeof(char), 1, fp)){
if(buf != ' ')
flag = 1;
else
if(flag){
flag=0;
word_count++;
}
}
if(flag)
word_count++;
fclose(fp);
printf("Count of words: %d\n", word_count);
return 0;
}