I have spent hours checking my code but still couldn't figure out which part went wrong. The program executed without throwing errors but the misspelled words identified were wrong. I think the problem is probably somewhere in my load or check function but I couldn't find out where the problem is.

I have removed some parts of the code and comments which I think are not that relevant to keep this within the 'reasonable' actions. Please tell me if I need to remove more of it (or moderators just edit for me).

Below is my load function (using trie) :

bool load(const char* dictionary)
// load dictionary

root = malloc(sizeof(node));
node* cursor = NULL;

    cursor = root;

    for (int c = fgetc(dic); c != '\n'; c = fgetc(dic))
        if (isalpha(c))
            if (cursor->children[c - 'a'] == NULL)
                cursor->children[c - 'a'] = malloc(sizeof(node));
            cursor = cursor->children[c - 'a'];            

        else if (c == '\'')
            if (cursor->children[26] == NULL)
                cursor->children[26] = malloc(sizeof(node));
            cursor = cursor->children[26];

    cursor->is_word = true;
} while (fgetc(dic) != EOF);

// close file

When run directly using ./speller, I would receive the following result :

WORDS IN TEXT:        19190
TIME IN load:         0.04
TIME IN check:        0.01
TIME IN size:         0.00
TIME IN unload:       0.04
TIME IN TOTAL:        0.09

It seems all the words in dictionary and text are calculated correctly when I compared it with staff's solution, but the words identified as misspelled are wrong.

Edit : Removed check function as it's not relevant.

And the following result when I used check50 :

:) dictionary.c, dictionary.h, Makefile exist
:) speller compiles
:( handles most basic words properly
   \ expected output, but not "\nMISSPELLED WORDS\n\nThe\nquick\nfox\n..."
:) handles min length (1-char) words
:) handles max length (45-char) words
:) spell-checking is case-insensitive
:) handles possessives properly
:( handles substrings properly
   \ expected output, but not "\nMISSPELLED WORDS\n\nca\ncats\ncaterpi..."

So which part of the code is wrong? I'm quite clueless and really appreciate your help in pointing out where I should change / look for the problem.

Thank you.

  • What's the actual code in place of // if it's not a word ... – Kareem Mar 28 '15 at 16:14
  • Just 'else', since the trie contains only a-z and apostrophe, so I think it's safe to assume anything other than those isn't a word in the dictionary. – Billy Mar 28 '15 at 16:21
  • Just to avoid wrong assumptions, please update your question with the full code of load and check! – Kareem Mar 28 '15 at 16:41
  • I've updated my code, only left out the open and close of dictionary file since it's not relevant (and based on the output, it's obvious that the file was opened successfully to load the words). Thanks for checking out the problem. – Billy Mar 28 '15 at 16:50

I think the problem is because, when inserting, you're checking and/or allocating memory for cursor->children[c - 'a']. Now if c is an uppercase char, that's a problem.

Update: I don't really know how I couldn't see that before, but it happens. You're skipping chars when using fgetc in the condition of the do while loop. Also, you're not checking for the EOF inside the for loop. I fixed these errors and got all the green smiley faces.

| improve this answer | |
  • Thanks for helping but the dictionary consists of lowercase alphabets only, so there's no need to make it case-insensitive when I load it into the trie. Any other thoughts? I'm really stuck here. – Billy Mar 29 '15 at 2:59
  • @Billy try emailing me with dictionary.c and dictionary.h,! – Kareem Mar 29 '15 at 9:54
  • I've already sent them to you. Thanks for checking them! Hoping to hear some hints to solve my problem from you soon. – Billy Mar 29 '15 at 10:11
  • @Billy please look at the Update section! – Kareem Mar 29 '15 at 11:01
  • Thanks for the hints, finally I've solved the problem :) – Billy Mar 29 '15 at 12:26

You must log in to answer this question.

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