I've used a dictionary I created and it's got 8 words. However, speller doesn't recognize the last one (it counts 7), but also it returns two words misspelled, even though all of them begin with capital letters (one word is my name and the other one is SummRe, instead of "summer"; the last one being "Brekon" instead of "broken"). However, when I run speller with austinpowers, it counts in all the words, but there about 200 misspelled words that it does not recognize as such.

EDIT: Fixed a couple of problems, now I know that I should review the apostrophe and some substrings.

Here are my load and check:

[code hidden per academic honesty guidelines]

I would like to add that when I run check50, it tells me I'm not handling possessives and substrings properly. However, some substrings are detected, cats caterpillars, for example.

Thank you!!

  • There's not nearly enough code here to understand the problem. From your description, you have issues with some decision code somewhere. You could have a problem with the way the dictionary is being built in load or a problem in search, or both. Is dictionary not loading the last word or just not counting it? There are just too many possible issues here.
    – Cliff B
    Feb 11, 2016 at 18:18
  • I've added it. Sorry.
    – Lana
    Feb 11, 2016 at 18:46

2 Answers 2

point->children[tolower(ch) - 'a'] = malloc(sizeof(node));
point->is_word = false;

When you set is_word to false, point is still pointing at the "letter" before in the trie, so you are then throwing away the knowledge of that previous letter as the end of a word.

Say you load "cat" then "cats". When you check whether the 't' node points to an s node, you find it doesn't, so you malloc a node for 's', but then you set the 't' node to is_word = false.

That means when you check the word cat, it will be misspelled.

I'd suggest using calloc rather than malloc: calloc(1,sizeof(node); and removing that is_word = false statement (in both places).

  • That's it! I cannot believe it! All green ones! And someone told me to put is false, when I asked for help elsewhere! I knew there was a problem with malloc, because when I printed out the word, I was getting a string and a bunch of garbage values. Thank you, thank you!
    – Lana
    Feb 23, 2016 at 23:17
  • Glad to help. I've edited your post to hide the code behind the 'edit' flag, so as to avoid spoilers for others.
    – curiouskiwi
    Feb 23, 2016 at 23:41

I see a couple of conspicuous issues. First, the code increments wordcount for every character read in, not every word. That's going to throw your word count way off.

Second, when you malloc new nodes, you don't initialize the is_word element of the node. This means that it contains garbage data that could be interpreted as true or false. is_word really needs to be initialized as false whenever a node is created.

Of course, there's no way to tell what, if anything, might be wrong with check(). This should get you going a little.


There are problems in both load() and check(). Let's start with check().

In check, you have a catchall return statement at the end - return true;. When a function is validating something, as this one does, it should ONLY return TRUE when the test conditions have been verified as met. This catchall return should be FALSE, not true.

Next, the code is checking for the end of the word by looking for the end of string marker, \0. It never gets there. The for loop is limited at strlen(word). Unfortunately, the strlen() function does not include the EOS marker, only the characters leading up to it. You are stopping the loop one char too early, so it never actually finds a word.

Moving on to loop(). There are problems here too. First, it doesn't handle the last word correctly. Since there is no '\n' after the last word, it never gets to a point where it can mark the end of the word and increment the word count.

Now is where it gets fun. There are other problems that I've not been able to isolate. For instance, after processing a dictionary with two words, I can walk the trie and find 27 end of word markers, the first word doesn't appear to be marked as a word, etc.

This should at least get you going again. I'll leave it to you to debug load() and find the root causes.

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

  • I put word_count inside of the if statement that checks is_word. Yes, I don't know how I forgot that, I found somewhere while I was looking for the answer, that we should also initialize the is_word. I tried to calloc a node point, which gave me seg fault, so I added point->is_word malloc, but nothing happened. 0 words in dictionary, still not enough misspelled words, words in text is fine.
    – Lana
    Feb 11, 2016 at 19:35
  • Not malloc, I wrote point->is_word = false; *
    – Lana
    Feb 11, 2016 at 19:44
  • Still not enough code to see the problems. Please update load and add check to show current versions.
    – Cliff B
    Feb 21, 2016 at 19:09
  • I've tried before with what you've said for check - it only gets worse in terms of misspelled words detected (when I return false in the end. The load itself counts well the words loaded from the dictionary/text. However, I've tried once again changing the check function as you told me to, and now check50 handles possessives correctly, but finds too many words misspelled. The first check (before this edit) gave me this sandbox.cs50.net/checks/20a340ef81f04b7c8fd89b9e89862dcf , while the second gave me this sandbox.cs50.net/checks/555eb0c13a05489eabcc8bbb27b80697
    – Lana
    Feb 23, 2016 at 16:22
  • First, just because the numbers are getting worse doesn't mean that the code got worse. It's more important that the check function gets more accurate. Once I made the right changes to check() and plugged it into my code, it was perfect. But, if load isn't right, there's no way to know if check will work right, even if it's written correctly. There are still serious problems in the load code. Frankly, I spent a couple hours working on it and couldn't isolate the issues, but load still needs to be cleaned up.
    – Cliff B
    Feb 23, 2016 at 16:40

You must log in to answer this question.

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