#include #include #include #include "genlib.h" #include "strlib.h" #include "simpio.h" #define MAXWORDS 32000 #define DICTFILENAME "/usr/lib/dict/words" #define MATCH_ANY '?' typedef string dict_type[MAXWORDS]; int read_dict_file(char *filename, dict_type dict) { int numwords; FILE *infile; string next_word; infile = fopen(filename, "r"); if (infile == NULL) Error("Could not open file `%s'\n", filename); numwords = 0; while (numwords < MAXWORDS && (next_word = ReadLine(infile)) != NULL) dict[numwords++] = next_word; fclose(infile); return(numwords); } bool match_word(string pattern, string word) { int i; if (StringLength(pattern) != StringLength(word)) return(FALSE); for (i = 0; i < StringLength(pattern); i++) if (IthChar(pattern, i) != IthChar(word, i) && IthChar(pattern, i) != MATCH_ANY) return(FALSE); return(TRUE); } void match_dict(string 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\n", dict[i]); found_a_match = TRUE; } if (!found_a_match) printf("No words were found that match %s\n", pattern); } main() { dict_type dict; int numwords; string pattern; numwords = read_dict_file(DICTFILENAME, dict); for (;;) { printf("Please enter a pattern: "); fflush(stdout); pattern = GetLine(); if (pattern == NULL) break; match_dict(pattern, dict, numwords); } printf("\nGoodbye.\n"); }