#include #include #include #define MAXWORDS 32000 #define MAXWORDLEN 32 #define DICTFILENAME "/usr/lib/dict/words" #define MATCH_ANY '?' #define FALSE 0 #define TRUE 1 typedef int BOOL; typedef char word_type[MAXWORDLEN]; typedef word_type dict_type[MAXWORDS]; int read_dict_file(char *filename, dict_type dict) { int numwords; FILE *infile; infile = fopen(filename, "r"); if (infile == NULL) { fprintf(stderr, "Could not open file `%s'\n", filename); exit(EXIT_FAILURE); } numwords = 0; while (numwords < MAXWORDS && fgets(dict[numwords], MAXWORDLEN, infile) != NULL) numwords++; fclose(infile); return(numwords); } BOOL match_word(word_type pattern, word_type word) { int i; if (strlen(pattern) != strlen(word)) return(FALSE); for (i = 0; i < strlen(pattern); i++) if (pattern[i] != word[i] && pattern[i] != MATCH_ANY) return(FALSE); return(TRUE); } void match_dict(word_type pattern, dict_type dict, int numwords) { int i; BOOL found_a_match; found_a_match = FALSE; for (i = 0; i < numwords; i++) if (match_word(pattern, dict[i])) { printf("%s", dict[i]); found_a_match = TRUE; } if (!found_a_match) printf("No words were found that match %s", pattern); } main() { dict_type dict; int numwords; word_type pattern; numwords = read_dict_file(DICTFILENAME, dict); for (;;) { printf("Please enter a pattern: "); fflush(stdout); if (fgets(pattern, MAXWORDLEN, stdin) == NULL) break; match_dict(pattern, dict, numwords); } printf("\nGoodbye.\n"); }