/* 'strstr' searches for a substring in a string */ #include #include int main() { char string1[1024]; char strings[20][10]; char *position; FILE *fileptr; int i, j; /* open the file of dictionary words */ fileptr = fopen("words.txt", "r"); /* read the 10 words in the file into the 2D array of characters, treating them as a 1D array of strings */ for (i = 0; i < 10; i++) { fgets(strings[i], 10, fileptr); /* now go through each string and get rid of the newline character at the end of the line, replacing it with a NULL */ for (j = 0; j < 10; j++) if (strings[i][j] == 10) strings[i][j] = 0; /* print them off just to be sure everything's ok */ printf("Read string: %s\n", strings[i]); } fclose(fileptr); /* open the file containing the large string */ fileptr = fopen("large_string.txt", "r"); /* grab it */ fgets(string1, 1024, fileptr); fclose(fileptr); /* now look through the large string for each word in the list */ for (i = 0; i < 10; i++) { printf("Searching for string %s...\n", strings[i]); position = strstr(string1, strings[i]); if (position != NULL) printf("Found string %s at position %d\n", strings[i], position); } }