1

I've been running the code with the small dictionary and a sample text of "cat hgsgk caterpillar", so the code should theoretically return a wordcount of 3, and hgsgk as the only misspelled word. However, the code I have currently outputs a wordcount of 2, and lists "cat" and "hgsgk" as the misspelled words. I have been checking my code over and over, and I really have no idea what's wrong! I think it's an issue with my check function, but there may also be a problem with the load function.

// Global variables
node *first;
int wordcount = 0;

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    // Create ptr to search through dictionary
    node *ptr =  first;

    // Iterate through each letter, use ptr to follow path in dictionary
    for (int i = 0; i < strlen(word); i++)
    {
        // Find numerical value for i
        int value;

        if (islower(word[i]))
        {
            value = word[i] - 'a';
        }
        else if (isupper(word[i]))
        {
            value = word[i] - 'A';
        }
        else
        {
            // Assume if not upper or lower, must be apostrophe
            value = 26;
        }

        // If end of word, check if is in dictionary
        if (i == (strlen(word) - 1))
        {
            if (ptr->is_word)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        // Otherwise, check if links to next letter
        else if (ptr->children[value])
        {
            ptr = ptr->children[value];
        }
        else
        {
            return false;
        }
    }

    // Return false if error
    return false;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // Creates root of trie
    first = calloc(1, sizeof(node));
    if (!first)
    {
        return false;
    }

    // Create ptr, set equal to first
    node *ptr = first;

    // Opens dictionary
    FILE *opened = fopen(dictionary, "r");
    if (opened == NULL)
    {
        return false;
    }

    // Add each character to trie or marks end of word
    for (int i = fgetc(opened); i != EOF; i = fgetc(opened))
    {
        // Checks for end of word
        if (!isspace(i))
        {
            // Find numerical value for i
            int value;

            if (islower(i))
            {
                value = i - 'a';
            }
            else if (isupper(i))
            {
                value = i - 'A';
            }
            else
            {
                // Assume if not upper or lower, must be apostrophe
                value = 26;
            }

            // Goes to corresponding pointer in children array and creates new node if necessary
            if (ptr->children[value] == NULL)
            {
                node *next_letter = calloc(1, sizeof(node));
                ptr->children[value] = next_letter;

                // Check next letter was successful
                if (!next_letter)
                {
                    return false;
                }
            }

            // Move pointer to next node
            ptr = ptr->children[value];
        }
        else
        {
            // Indicate end of word
            ptr->is_word = true;

            // Increase wordcount
            wordcount++;

            // Return ptr to beginning
            ptr = first;
        }
    }

    // Close file, and report success!
    fclose(opened);
    return true;
}

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

1 Answer 1

1

The if (ptr->is_word) must happen after the ptr = ptr->children[value] part. You could even avoid the i == (strlen(word) - 1) check and move it to after the loop (which I would prefer).

1
  • Thank you so much! That makes a lot of sense
    – dsaba123
    Commented May 29, 2019 at 22:30

You must log in to answer this question.

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