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
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *fp;
char buf = ' ';
int word_count=0, char_count = 0;
if((fp = fopen("text.txt", "r")) == NULL)
printf("Error to open file");
while(fread(&buf, sizeof(char), 1, fp)){
if(buf != ' ')
char_count++;
else {
if(char_count)
word_count++;
char_count=0;
}
}
if(char_count)
word_count++;
fclose(fp);
printf("Count of words: %d\n", word_count);
return 0;
}