Count lines, words and keywords in a text file
the exercise is to count the number of lines, words and keywords present in a text file. i was able to get the lines and the words counted, but i was unable to count the KEYWORDS present in the text file. there are 4 different keywords present in the text. could anyone help me with it. Thanks in advance. my code is: #include <stdio.h> int main() { FILE *fp; char filename[100]; char ch; int linecount, wordcount; linecount = 0; wordcount = 0; printf("Enter a filename :"); gets(filename); fp = fopen(filename,"r"); if ( fp ) { while ((ch=getc(fp)) != EOF) { if (ch == ' ' || ch == '\n') { ++wordcount; } if (ch == '\n') { ++linecount; } } if (wordcount > 0) { ++linecount; ++wordcount; } } else { printf("failed to open the file\n"); } printf("Lines : %d \n", linecount); printf("Words : %d \n", wordcount); return(0); }