0

My check function does not get the correct amount of misspelled words. It's always higher than it should be. I figure there must be something wrong with my logic but I can't figure out what. I get the correct amount of words in the dictionary so I think my load function is okay, though the error could be in there as well.

Could someone take a look at my code and see if anything pops out as being a potential issue? Thanks!

 /**
 * Implements a dictionary's functionality.
 */

#include <stdbool.h>
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>

#include "dictionary.h"

int words = 0;
node *memory;
node *current;

/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char *word)
{
    current = root;
    int index = 0;

    int c = 0;
    while(word[index] != '\0')
    {
        if(isalpha(word[index]))
        {
            c = tolower(word[index]) - 'a';
        }
        else
        {
            c = 26;
        }

        if(current->alphabet[c] != NULL)
        {
            current = current->alphabet[c];
            index++;
        }
        else
        {
            return false;
        }
    }


    if(current->is_word == true)
    {
        return true;
    }
    else
    {
        return false;
    }
}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
bool load(const char *dictionary)
{
    memory = calloc(500000, sizeof(node));
    node *nextspot = memory;

    FILE *dict = fopen(dictionary, "r");

    if(dict == NULL)
    {
        return false;
    }

    fseek(dict, 0, SEEK_END);
    long size = ftell(dict);
    fseek(dict, 0, SEEK_SET);

    char *dict2 = malloc(size + 1);
    fread(dict2, 1, size, dict);
    fclose(dict);
    dict2[size] = '\0';

    root = nextspot++;
    current = NULL;
    char *dict3 = dict2;

    while(*dict3)
    {
        current = root;
        for(; *dict3 != '\n' && *dict3; ++dict3)
        {
            if(*dict3 == '\'')
            {
                if(current->alphabet[26] == NULL)
                {
                    current->alphabet[26] = nextspot++;
                }
                else
                {
                    current = current->alphabet[26];
                }
            }
            else
            {
                if(current->alphabet[*dict3 - 'a'] == NULL)
                {
                    current->alphabet[*dict3 - 'a'] = nextspot++;
                }
                else
                {
                    current = current->alphabet[*dict3 - 'a'];
                }
            }
        }
        current->is_word = true;
        words++;
        if(*dict3 == '\n')
        {
            ++dict3;
        }
    }
    free(dict2);
    return true;
}

typedef struct node
{
    bool is_word;
    struct node *alphabet[27];
}
node;

node *root;
1

You need to move current along as you load each letter. Otherwise, current->alphabet[...] will always be looking at the root->alphabet[...] instead of root->alphabet[...]->alphabet[...]-> etc.

As well, for a style/design issue, you are allocating a lot of memory. You shouldn't need 112,000,000 bytes to load a 2 word dictionary, for example!

You must log in to answer this question.

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