0

working on check() function and my trie data type seems to load ok with the full dictionary but check is unable to find any mispellings for a text file. I beleive I'm not correctly returning false when the trie and passed in word do not match. I've tried to move the code around, and also to find another implementation of is_word == true, but I feel the current code I have should work... not sure why it's not.

would love some advice on this. Thank you so much!


// Returns true if word is in dictionary else false
bool check(const char *word)
{
    // TODO
    node* current = root;

    int wordlen, i, index;
    wordlen = strlen(word);


    // while(current != NULL)
    // {




    for(i = 0; i < wordlen; i++)
    {
        // int index = word[i] - 'a';
         if (word[i] == '\'')
        {
            //element children[26] is apostrophe character
             index = 26;
        }
        else if(isupper(word[i]))
        {
             index = word[i] - 'A';
        }
        else
        {
             index = word[i] - 'a';
        }

        if(current->children[index] == NULL)
        {

            return false;
        }

         //printf("%c", (current->children[index]));

        // if(word[wordlen-1] == wordlen-1 && current->is_word == true)
        // {
        //     printf("\n");
        //     return true;
        // }


      // continues to next letter and moves pointer to next node if element pointer is not null
        if (current->children[index] != NULL)
        {
         current = current->children[index];
            if (current->is_word == true)
            {
                return true;
            }
         }

    }

    return false;
}

1

Your code is returning true if the first few characters form a valid word, ignoring the rest.

The code

    if (current->is_word == true)
    {
        return true;
    }

should be after the loop, when current is at the node representing the full word.

You must log in to answer this question.

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