I'm struggling with pset 6 (quite difficult logical and implementation wise, but I like this level of difficulty and this challenge). I'm implementing load and I just found out that I'm 34 words off with the Austinpowers.txt text.

I have 19224 words, while CS50 reports there are 19190. I know what is setting it off, it's numbers mixed with letters (e.g. '9B' or '6C'). The implementation of my code simply checks if the last letter isalpha (that means once the cursor has reached the end of a word) and it does not double check if the word contains any numbers. I know that there is an easy fix if it's just 2 letters but I am confused as to how I could fix this with words where the number is at the beginning, e.g. '4BDA' or similar.

How could I fix this issue?

Here is part of my code:

if (isalpha(ch))
    if (curser->children[tolower(ch) - 'a'] == NULL)
        curser->children[tolower(ch) - 'a'] = (struct node*)malloc(sizeof(node));
        curser->end = false;
    curser = curser->children[tolower(ch) - 'a'];
    curser->previous = ch;

else if (ch == '\'')
    if (curser->children[26] == NULL)
        curser->children[26] = (struct node*)malloc(sizeof(node));
    curser = curser->children[26];
    curser->previous = ch;

    if (isalpha(curser->previous) || curser->previous == '\'')
        curser->end = true;
        printf("%i\n", totalwords);
        curser = root;

and here is my Trie:

typedef struct node
    bool end;
    struct node* children[27];
    int previous; 
} node;

Any suggestions and improvements to my code are greatly appreciated! I'm still a novice to coding but I'm trying to get better at it and try to find the best implementation. :)


I can't see your pastebin but I'm guessing a for loop to iterate through all of the chars of the word to check for isalpha or if it is an apostrophe would take care of this.

\\for(int i = 0; i < the length of the word you are checking; i++)
>   {
>       //if word[i] isalpha or an apostrophe
>       //do something
>       //else
>       //do something else
>   }
  • The problem is that I do not safe the word in a buffer so I cannot iterate through it in total and do X if the 2nd char of the word does not equal isalpha. – user175422 Sep 6 '14 at 12:37
  • after reviewing the code provided by prof. Malan, it's already excluding words with digits so I'm not sure if the issue is what you think it is. – lethaljd Sep 6 '14 at 20:23
  • I have a done a small test with a text file that had 1 word with a digit in it and the other 3 were normal words. My program found 4 words while the CS50 program only detected 3. So I'm 98% sure that the digit detection is the problem here. – user175422 Sep 7 '14 at 21:06
  • Did you modify the speller.c file at all? The line starting with // ignore words with numbers (like MS Word) Should be skipping those words. They shouldn't even get to the point where the check function is called. – lethaljd Sep 7 '14 at 21:17

You must log in to answer this question.

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