0

When I run my program, the program output every single word in the text instead of solely misspelled words. I'm fairly confident the problem lies within my load function. Can someone identify anything wrong with my logic? Thank you!

bool load(const char *dictionary) { // TODO FILE *fp = fopen(dictionary, "r"); if (fp == NULL) { printf("Could not open %s.\n", dictionary); unload(); return 1; }

node root = {false,{NULL}};
node *ptr = &root;

for (int c = fgetc(fp); c != EOF; c = fgetc(fp))
{
    if (isalpha(c))
    {
        if (ptr->children[c - 97] == NULL)
        {
            node *new = malloc(sizeof(node));
            *new = (node) {false,{NULL}};
            ptr->children[c - 97] = new;
            ptr = new;
        }

        else
        {
             ptr = ptr->children[c - 97];
        }
    }

    else if (c + '0' == '\n')
    {
        ptr->end_word = true;
        ptr = &root;
    }
}
fclose(fp);
return true;

}

0

I see three problems here, at least:

1) How is check() going to know about your trie? root is declared locally here in load().

2) You're not taking apostrophes into account ( that will not cause for all words to be listed as misspelled, but still)

3) I can't possibly understand what the condition of the else if means. Why else if (c + '0' == '\n') ? What's c + '0' supposed to be?

You must log in to answer this question.

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