0

I am testing my load() with ./speller texts/austinpowers.txt and comparing the result with stuff version with stuff version ~cs50/pset5/speller texts/austinpowers.txt

I have been stuck here for a while.. Any help will be appreciated!!!

my result

enter image description here

stuff version

enter image description here

my load()

bool load(const char* dictionary)
{
    root = calloc(1, sizeof(NODE));
    NODE* ptr = root;

    FILE* dict = fopen(dictionary, "r");
    if (dict == NULL)
        return false;

    for (int c = fgetc(dict); c != EOF; c = fgetc(dict))
    {
        if(c != '\n' && c != '\'')
            c = c - (int)'a';
        else if (c == '\'')
            c = 26;

        if(c != '\n')
        {
            if(ptr->children[c] == NULL)
            {
                NODE* new_node = calloc(1, sizeof(NODE));
                ptr->children[c] = new_node;
                ptr = new_node;
            }
            else
            {
                ptr = ptr->children[c];
            }
        }
        else
        {
            ptr->is_word = true;
            ptr = root;
            count++;
        }
    }

    fclose(dict);
    return true;
}

1 Answer 1

1

Interesting interaction. The code is dependent on finding \n to mark the end of a word in the trie. The problem is that the ASCII code for \n is 10. The code that looks for \n comes after the letters have been changed to numbers between 0 and 26. Unfortunately, k is represented by 10.

If you look, your word count is too high by 10020. There also happen to be 10020 k's in the dictionary. Hmmm.....

You could test with a small dictionary as long as it has at least one word with a k in it.

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

1
  • Awesome!! My tiny dictionary had only cats and dogs but no kangaroo.. Sep 9, 2016 at 0:37

You must log in to answer this question.

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