1

I need help. When I check my codes through check50, I got a result like below

:) dictionary.c and dictionary.h exist :) speller compiles :( handles most basic words properly expected "MISSPELLED WORD...", not "MISSPELLED WORD..." :) handles min length (1-char) words :) handles max length (45-char) words :) handles words with apostrophes properly :) spell-checking is case-insensitive :( handles substrings properly expected "MISSPELLED WORD...", not "MISSPELLED WORD..." :| program is free of memory errors can't check until a frown turns upside down

So besides memory leak problem, I have two problems which are handling basic words and substrings (the ones I got frown face)

I think my load and check functions have something to do with the problems I have. But I have no idea what's wrong with it.

Here's my dictionary.c. Please help me to figure it out.

// Represents a trie
node *root;

// pointer for current node
node *current_node;


// hash the letter to put into children
unsigned int hash(const char c)
{
    if (c == '\'')
    {
         return 26;
    }
    else
    {
        return tolower(c) - 'a';
    }
}

// Free nodes , for unload function
bool freenode(node *ptr)
{
    for (int i = 0; i < N; i++)
    {
        if (ptr->children[i] != NULL)
        {
            freenode(ptr->children[i]);
        }
    }
    free(ptr);
    return true;
}

// Initializes the counter of words for size function
unsigned int word_count = 0;

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // Initialize trie
    root = malloc(sizeof(node));
    if (root == NULL)
    {
        return false;
    }
    root->is_word = false;
    for (int i = 0; i < N; i++)
    {
        root->children[i] = NULL;
    }

    // Open dictionary
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        unload();
        return false;
    }

    // Buffer for a word
    char word[LENGTH + 1];

    current_node = root;

    // Insert words into trie
    while (fscanf(file, "%s", word) != EOF)
    {
        for (int j = 0; j < strlen(word); j++)
        {
            int i = hash(word[j]);

            if (current_node->children[i] == NULL)
            {
                current_node->children[i] = malloc(sizeof(node));
                current_node = current_node->children[i];
            }
            else
            {
                current_node = current_node->children[i];
            }
        }

        current_node->is_word = true;
        word_count++;
    }

    // Close dictionary
    fclose(file);

    // Indicate success
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    return word_count;
}

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    current_node = root;

    // traversing a trie
    for (int j = 0; j < strlen(word); j++)
    {
        int i = hash(word[j]);
        if (current_node->children[i] == NULL)
        {
            return false;
        }
        else
        {
            current_node = current_node->children[i];
        }
    }

    // Once at end of input word, check if is_word is true
    if (current_node->is_word == true)
    {
        return true;
    }

    return false;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    if (word_count == 0)
    {
        return false; // cause it means 'unloaded'
    }
    else
    {
        freenode(root);
    }

    return true;
}

1 Answer 1

3
current_node = root;

should be within your while loop, so you start at the root for every new word. Otherwise a dictionary of cat caterpillar would end up with two words, cat and catcaterpillar.

Whenever you malloc, don't forget to initialise the structure.

2
  • Wow! I put current_node = root; into while loop and I fixed my problems !! haha thx But I have memory leak problem ....could you tell me more about initializing the structure whenever I malloc? I find it hard to understand.
    – Raebbit
    Feb 23, 2019 at 2:51
  • Oh never mind !! I fixed malloc problem!!!!!! thank you
    – Raebbit
    Feb 23, 2019 at 3:45

You must log in to answer this question.

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