0

I know there are some similar questions, but I can't figure this out. I have checked my check program by running printf functions through it, and it does iterate through all the letters properly (for example, for cat it iterates through c-a-t, but with fghjll it processes f- and then finds it's not in the trie). Still, ALL words are printed as misspelled words, so I figure there must be something wrong with my boolean in load. However, I can't quite figure out where the mistake lies.

This is my check (in which ALPHA is 27):

bool check(const char* word)

    {
        node* strider = root;
        int letter;

        for (int i = 0, size = strlen(word); i < size; i++)
        {
            if (isalpha(word[i]))
            {
                letter = tolower(word[i]) - 'a';
            }

            else if (word[i] == '\'')
            {
                letter = ALPHA - 1;
            }

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

        }
        return strider->is_word; 
    }

And this is my load:

bool load(const char* dictionary)
{
    // defining root, strider and wordcount
    // calloc sets to NULL, so avoiding garbage values
    root = calloc(1, sizeof(node));
    node* strider = root;

    int wordcount = 0;

    // open the dictionary
    FILE* dic = fopen(dictionary, "r");

    if (dic == NULL)
    {
        printf("Could not open the dictionary file.\n");
        return false;
    }

    // iterate through all characters in dictionary 
    for (int c = fgetc(dic); c != EOF; c = fgetc(dic))
    {
        // place value of apostrophe in line with letters
        if (c == '\'')
        {
            c = ('z' + 1);
        }

        // set the value for the characters
        int index = tolower(c) - 'a';

        // create new nodes if not already existing
        // else go to next node
        if (strider->children[index] == NULL)
        {
            node* newnode = calloc(1, sizeof(node));
            strider->children[index] = newnode;
        }
        strider = strider->children[index];

        // when end of word, set bool to true and "reset"
        if (c == '\n')
        {
            strider->is_word = true;
            strider = root;
            wordcount++;
        }
    }
    fclose(dic);
    return true;
}

Help please? Thank you!

1 Answer 1

1

You're on the right track - there is something wrong in load(). It's a subtle logic problem. When you process each character, the following line is processed:

    strider = strider->children[index];

But what happens when you're processing '\n'? At best, it's putting another step in the linked list for the word. Shouldn't it instead be setting is-word?

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

1
  • I figured it out! It's all tidied up and running now, cheers! :)
    – Saar Hoek
    Oct 7, 2016 at 14:44

You must log in to answer this question.

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