0

I haven't been able to figure out why my code recognizes every word with apostrophes as a misspelled word. I was hoping someone can help me spot any problems with my code. Trying not to break the academic honesty policy, here are some relevant parts of my code.

this is where I retrieve words from dictionary:

//retrieve words from dictionary
    int letter;

    while (EOF != (letter = fgetc(dict)))
    {
        insert(letter, dict);
        words++;
    }

this is where I insert words to the trie.

//insert word into trie
void insert(int letter, FILE* dict)
{

    //took idea from a forum
    int aint = (int)'a';
    int zint = (int)'z';

    node* trav = root;

    for (; letter != '\n'; letter = fgetc(dict))
    {
        if (letter == '\'')
        {
            letter = zint + 1;
        }
        if (trav->children[letter - aint] == NULL)
        {
            trav->children[letter - aint] = newnode();
        }

        trav = trav->children[letter - aint];
    }
    trav->is_word = true;
}

and this is my check function

bool check(const char* word)
{
    int len = strlen(word);
    char word_low[len + 1];
    strcpy(word_low, word);

    node* trav = root;

    int letter;
    int aint = (int)'a';

    //convert to lowercase
    for (int l = 0; l < len; l++)
    {
        word_low[l] = tolower(word_low[l]);
    }

    for (int i = 0; i < len; i++)
    {
        letter = (int)word_low[i];

        if (trav->children[letter - aint] == NULL)
        {
            return false;
        }

        trav = trav->children[letter - aint];
    }

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

    return false;
}

I hope someone can help me! It's the only thing left for 'speller' to be ready to submit.

0

Simple. You did what looks like a good job of accounting for apostrophes in the insert() function, but completely forgot to do the same in the check() function.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Thanks! I totally didn't see that. It works perfectly now. – Carlos C Nov 17 '16 at 3:16

You must log in to answer this question.

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