/** * Implements a dictionary's functionality. */ #include <stdbool.h> #include "dictionary.h" #include<string.h> #include <stdio.h> #include <stdlib.h> #include<ctype.h> int count=0; node *root=NULL; /** * Returns true if word is in dictionary else false. */ bool check(const char *word) { for(int i=0;word[i]!='\0';i++) tolower(word[i]); node *tmp; tmp=root; for(int i=0;word[i]!='\0';i++) { int a; if(isalpha(word[i])) a=word[i]-97; else a=26; if(tmp->children[a]==NULL) return false; else tmp=tmp->children[a]; } if(tmp->is_word==true) return true; else return false; } /** * Loads dictionary into memory. Returns true if successful else false. */ bool load(const char *dictionary) { node *newnode; FILE* file = fopen(dictionary, "r"); if (file == NULL) { printf("Could not open dictionary.\n"); return false; } root= calloc(1, sizeof(node)); while (!feof(file)) { char word[LENGTH+1]; fscanf(file,"%s\n",word); count++; node *tmp; tmp=root; for(int i=0;word[i]!='\0';i++) { int a; if(isalpha(word[i])) a=(int)word[i]-97; else a=26; if(tmp->children[a]==NULL) { newnode=calloc(1, sizeof(node)); tmp->children[a]=newnode; tmp=newnode; } else if(tmp->children[a]!=NULL) { tmp=newnode; } } tmp->is_word=true; } // TODO fclose(file); return true; } /** * Returns number of words in dictionary if loaded else 0 if not yet loaded. */ unsigned int size(void) { // TODO if(count!=0) return count; else return 0; } /** * Unloads dictionary from memory. Returns true if successful else false. */ bool unload(void) { if(root==NULL) return false; for(int i=0;i<27;i++) if(root->children[i]!=NULL) delNode(root->children[i]); return true; } void delNode(node *tempNode) { for (int i=0;i<27;i++) { if (tempNode->children[i] != NULL) delNode(tempNode->children[i]); } free(tempNode); } ---------- **OUTPUT** ---------- ~/workspace/pset5/speller/ (master) $ ./speller texts/ralph.txt MISSPELLED WORDS When<br> I<br> grow<br> up<br> I<br> want<br> to<br> be<br> principal<br> or<br> caterpillar<br> I<br> love<br> you<br> Principal<br> Skinner<br> Ralph<br> Wiggum<br> WORDS MISSPELLED: 18 <br> WORDS IN DICTIONARY: 143091<br> WORDS IN TEXT: 20<br> TIME IN load: 0.18<br> TIME IN check: 0.00<br> TIME IN size: 0.00<br> TIME IN unload: 0.17<br> TIME IN TOTAL: 0.35