could you please teach me how to make sure if i load the dictionary successfully. when i run check, no word misspelled.. Thank you.
typedef struct node
{
bool is_word;
struct node *children[27];
} node;
node* root=NULL;
// Returns true if word is in dictionary else false
bool check(const char *word)
{
// TODO
int j=0;
int k;
for (k=0;k <= strlen(word) +2;k++)
{
for (node *travs=root;travs!=NULL;travs=travs->children[j])
{
if(isalpha(word[k]) || word[k]=='\'')
{
j=tolower(word[k])-'a';
if(j==-58)
{
j=26;
}
if (travs->children[j]==NULL)
{
return false;
}
else if (travs->children[j]!=NULL)
{
travs=travs->children[j];
}
} //end of if(isalpha(letter) || letter=='\'' )
}//end of (node *travs=root;travs!=NULL;travs=travs->children[j])
if(word[k]=='\0')
{
for (node *travs=root;travs!=NULL;travs=travs->children[j])
if (travs->is_word==true)
{
return true;
k=0;
}
}
}// end of for (k=0;k<strlen(word),k++)
return false;
}
bool load(const char *dictionary)
{
// TODO
root = calloc(1, sizeof(node));
FILE *inptr=fopen(dictionary,"r");
int i=0; //for children[i]
if(inptr==NULL)
{
printf("Error opening file");
return 0;
}
char letter='\0';
while (true)
{
letter=fgetc(inptr);
if (letter==EOF)
{
break;
}
for (node *ptr=root; ptr != NULL; ptr=ptr->children[i])
{
if(isalpha(letter) || letter=='\'' )
{
i = tolower(letter) - 'a';
if (i==-58) //ASCII code of '\'' - 'a'
{
i=26;
}
//check children[i]’s value, if the value in children[i] is NULL,
calloc a new node, then let children[i] point to it
if(ptr->children[i]==NULL)
{
ptr->children[i] = calloc(1, sizeof(node));
ptr=ptr->children[i];
}
else if (ptr->children[i]!=NULL)
{
ptr=ptr->children[i];
}
}// end of if(isalpha(letter) || letter=='\'' )
else if(letter=='\n')
{
ptr-> is_word=true;
}
} // end of for(node *ptr=root; ptr != NULL; ptr=ptr->children[index])
} //end of while
fclose(inptr);
return true;
} // end of bool load(const char *dictionary)