0

I have been spending loads of time staring at my code for loading a dictionary and checking for misspelled words. Te programme keeps telling me that all words are misspelled which leads me to believe that I am not passing the loaded dictionary on to the 'check' function. But I feel like I am doing this by declaring the root and the cursor globally. Could somebody please have a look and tell me what is going wrong? Thank you so much!!

declare variables (outside any function)

//define a struct for the nodes in the trie
typedef struct node
{
    bool is_word;
    struct node* childeren[27];
}
node;
node* root = NULL;
node* cursor = NULL;
int wordCount = 0;

load function

// Loads dictionary into memory. Returns true if successful else false.
...

// set root and cursor
root = calloc(1, sizeof(node));
root -> is_word = false;
char c = 0;
// go over all the letters in the dictionary
    while ((c =fgetc(dict)) != EOF)
    {
        //set the cursor to the root at the beginning of a word
        cursor = root;

        //loop over the word (until the "enter")
        for (; c != '\n'; c =  fgetc(dict))
        {
            //create new childeren if not there yet at letters
            if(cursor -> childeren[c - 'a'] == NULL)
            {
                cursor -> childeren[c - 'a'] = calloc (1, sizeof(node));
                cursor = cursor -> childeren[c - 'a'];
            }
            else
            {
                cursor = cursor -> childeren[c - 'a'];
            }

        }
        //if the end of a word is found (break out of the for loop)
        cursor -> is_word = true;
        wordCount++;
    }

    fclose (dict);
    return true;
}

check function

//Returns true if word is in dictionary else false.
bool check(const char *word)
{
    char c = 0;
    node* pointer = root;

    // For loop over the characters in the word
    for (int i = 0; i <= strlen(word); i++)
    {
        // make it case-insensative
        c = tolower(word[i]);

        if(c == '\'')
        {
            c = 26+'a';
        }

        // return false (misspelled) if the next letter is not loaded in the dictionary
        if(pointer -> childeren [c - 'a'] == NULL)
        {
            return false;
        }
        // move on to the next letter if loaded in dictionary and not at the end of the word yet
        else if (pointer -> childeren[c - 'a'] != NULL && i < strlen(word)-1)
        {
            pointer = pointer -> childeren[c - 'a'];
        }
        // return is_word true if last letter of the word is found
        else if ( i < strlen(word)-1 && cursor->is_word == true )
        {
            return true;
        }
    }
    return false;
}

1 Answer 1

0

One of your problems has to do with the apostrophe '.

You don't check for it in load(), and that is going to mess up some of your words.

The other mistake —and that's where your problems fully arise— is in the following code:

// return is_word true if last letter of the word is found
else if ( i < strlen(word)-1 && cursor->is_word == true )
{
    return true;
}

You say last letter, but you check for i < strlen(word)-1, while it should be i == strlen(word)-1.

Fix these, and report back if it works! Happy Coding!

1
  • 1
    Thank you so much! it was indeed just not checking the entire word. Stupid mistake but I think I would not have found it by myself. Fixed it now and can move on to the rest of the programme this weekend :)
    – Gary
    Sep 14, 2017 at 21:03

You must log in to answer this question.

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