Hello, everyone. I am a beginner of c programming. I try to write a program to count how many times a word appeared in a txt file. However, i find it stops when there is a new paragraph and it only return the number of the words in the first paragrah but not the whole text. Can anyone tell me what is going wrong in my program?
thank you very muchCode:#include <stdio.h> #include <string.h> #include <stdlib.h> #define SIZE 1000 char *search(char *, char *); int main() { char string1[SIZE], string2[SIZE], oriword[30], *newword, file1[30]; int count; FILE *inFile; printf("Please enter an input file name: "); scanf("%s", &file1); inFile = fopen(file1, "r"); printf("\nWhich word do you want to find: "); scanf("%s",&oriword); while (fgets(string1, SIZE, inFile) != NULL); newword = search(string1, oriword); count = 0; while (newword != NULL) { count++; newword = search(newword + strlen(oriword), oriword); printf("%s\n", newword); } printf("\nThe number of %c%s%c appeared %d time(s).\n", '"', oriword, '"', count); fclose(inFile); fflush(stdin); printf("\nPlease press the Enter key to continue......\n"); getchar(); return 0; } char *search(char *string3, char *owd) { int j, k; char *tmp; j = strlen(string3); k = strlen(owd); for (tmp = string3; tmp <= string3 + j - k; tmp++) { if ((tmp == string3 || *(tmp - 1) == ' ' || *(tmp - 1) == '.') && strncmp(tmp, owd, k) == 0 && (*(tmp + k) == ' ' || *(tmp + k) == '.' || *(tmp + k) == '\0')) return tmp; } return NULL; }



LinkBack URL
About LinkBacks


