0

please help me figure out why am getting wrong mispelt words and 0 size.below is is my load , check and size:

bool load(const char *dictionary)
{
// TODO
FILE *fp=fopen(dictionary,"r");
if(fp==NULL)
{
    printf("dictionary failled to load\n");
    unload();
    return false;
}

     char c;
    //scan dictionary word by word    
while((c=fgetc(fp))!=EOF)
{
    if(c=='\n')
       {
        trav->is_word=true;
        trav=&root_ptr;
        ++word_count;
       }
    else
         {

          int INDEX=tolower(c)-'a';
          if(trav->next_char[INDEX]==NULL)
           {
            trav->next_char[INDEX]=(node*)malloc(sizeof(node));
           }
           trav=trav->next_char[INDEX];
         }  
}

fclose(fp);
return true;
}
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char *word)
{
trav=&root_ptr;
for(int i=0;i<=strlen(word);i++)
{
 int c=word[i];
 int INDEX=tolower(c)-'a';
 if(trav->next_char[INDEX])
 trav=trav->next_char[INDEX];
 if(trav->is_word==true)
 {
 return trav->is_word;
 }
 }
 return false;

}
// * Returns number of words in dictionary if loaded else 0 if not yet 
loaded.

unsigned int size(void)
{
 //TODO
return word_count;
}
0

trav=&root_ptr? Is root_ptr a node or a node*? (ptr stands for "pointer", I assume you have some node root_ptr; as a global variable, which would not be a pointer)

Also, memory allocated with malloc needs to be initialised, it may contain any values that were previously stored at that memory address. You can for example use calloc instead of malloc to zero the memory on allocation.

You don't seem to handle the apostrophe separately. Words in this case may consist of upper- and lower-case letters and apostrophes '.

In check, you need to test whether the path you try to go actually exists, and return false otherwise. Move the return trav->is_word below the loop, to the end of the function. You should return that only after reaching the right node. Speaking of which, i<=strlen(word) lets you go too far. Use < instead of <=. And again, handle apostrophes.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .