#include #include #define SIZE 80 + 1 int getContents(char pattern[]); int countMatch(char contents[], char pattern[]); int main(void) { char pattern[SIZE]; printf ("\nPlease enter a pattern: "); scanf ("%s", pattern); printf ("Number of matches of the pattern \"%s\": %d\n\n", pattern, getContents(pattern)); return 0; } /* end of main program */ int getContents(char pattern[]) { FILE *fp; char line[SIZE]; int count = 0; fp = fopen ("file.txt", "r"); if (fp == NULL) printf ("Error: cannot open the data file\n\n"); else while ( fscanf(fp, "%[^\n]\n", line) == 1) { count += countMatch(line, pattern); } fclose(fp); return count; } /* end of getContents() function */ int countMatch(char contents[], char pattern[]) { int i,j,k; int count = 0; char temp[SIZE]; for (i = 0; i < strlen(contents); i++) { k = 0; for (j = i; j < (i + strlen(pattern)); j++) { temp[k++] = contents[j]; } temp[k] = '\0'; if (strcmp(temp, pattern) == 0) count += 1; } return count; } /* end of countMatch() function */