#include #include #include #include #include #include "fields.h" typedef struct { int count; } WC; main(int argc, char *argv[]) { ENTRY new_entry; if (argc != 2) { printf("usage: word_count input_file\n"); exit(1); } IS input = new_inputstruct(argv[1]); if (input == NULL) { perror(argv[1]); exit(1); } hcreate(20); while (get_line(input) >= 0) { int i; for (i = 0; i < input->NF; i++) { new_entry.key = input->fields[i]; ENTRY *entry = hsearch(new_entry, FIND); WC *value; if (entry == 0) { value = (WC *)malloc(sizeof(WC)); value->count = 1; new_entry.key = strdup(input->fields[i]); new_entry.data = value; hsearch(new_entry, ENTER); } else { value = (WC *)entry->data; value->count++; } } } char word[30]; printf("Enter word: "); while (scanf("%s", word) != EOF) { new_entry.key = word; ENTRY *value = (ENTRY *)hsearch(new_entry, FIND); if (value == 0) { printf("%s not found\n", word); } else printf("%s: %d\n", word, ((WC *)value->data)->count); printf("Enter word: "); } }