0

first time poster and new beginner.

I realize this question has been asked numerous times among different forums, but still after looking at other's code, I simply can't figure it out.

I have ran my program and for example with lalaland.txt, all of my words are showing as misspelled.

WORDS MISSPELLED: 17756

WORDS IN DICTIONARY: 143091

WORDS IN TEXT: 17756

TIME IN load: 0.07

TIME IN check: 0.01

TIME IN size: 0.00

TIME IN unload: 0.00

TIME IN TOTAL: 0.08

Any other stylistic feedback is very much appreciated.

Thank you in advance,

// Implements a dictionary's functionality

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

    #include "dictionary.h"


    // Represents number of children for each node in a trie
    #define N 27

    // Represents a node in a trie
    typedef struct node
    {
        bool is_word;
        struct node *children[N];
    }
    node;

    // Represents a trie
    node *root;

    //global function of delete
    void delete (node *node);

    //global variable of words counter
    int words = 0;

    // Loads dictionary into memory, returning true if successful else false
    bool load(const char *dictionary)
    {
        // Initialize trie
        root = calloc(1,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];

        // Insert words into trie
        while (fscanf(file, "%s", word) != EOF)
        {
            int wordlen = strlen(word);
            int letter;

            node *next = root;

            //iterates over each letter of word
            for (int i = 0; i < wordlen; i++)
            {
                //converts letter to number
                letter = word[i] % 32;
                if (word[i] == '\'')
                {
                    letter = 26;
                }

                //if letter pointer doesn't point to anything
                if (next->children[letter] == NULL)
                {
                    //mallocs new space for node and points children to it
                    next->children[letter] = calloc(1, sizeof(node));

                    //checks that malloc ran successfully
                    if (next->children[letter] == NULL)
                    {
                        printf("Unable to free memory\n");
                        return false;
                    }

                    //moves down pointer to next node
                    next = next->children[letter];

                }

                else
                //moves down pointer to next node
                next = next->children[letter];

                //if end of word, sets value to true
                if (i == wordlen - 1)
                {
                    next->is_word = true;

                    //increases word counter by 1
                    words++;
                }

            }

        }

        // 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)
    {
        //returns word counter
        return words;

        //returns 0 if no words are loaded
        if (words == 0)
        {
            return 0;
        }

    }

    // Returns true if word is in dictionary else false
    bool check(const char *word)
    {
        //creates cursor that initailly points to node root
        node *cursor = root;

        //sets wordlen equal to length of the input word
        int wordlen = strlen(word);

        //iterates over each letter of input word
        for (int i = 0; i < wordlen; i++ )
        {
            //creates variable of number corresponding to letter in alphabet (0-25)
            int letternum = word[i] % 32;

            // sets apostrophe symbol to letternum 26
            if (word[i] == '\'')
            {
                //element children[26] is apostrophe character
                letternum = 26;
            }

            //checks if element pointer is null
            if (cursor->children[letternum] == NULL)
            {
                return false;
            }

            //continues to next letter and moves pointer to next node if element pointer is not null
            else if (cursor->children[letternum] != NULL)
            {
                cursor = cursor->children[letternum];
                continue;
            }

            //returns true if is_word is also true
            if (cursor->is_word == true)
            {
                return true;
            }

        }

        return false;
    }

    // Unloads dictionary from memory, returning true if successful else false
    bool unload(void)
    {
        delete(root);
        return true;
    }

    //recurisve function to delete nodes
    void delete (node *node)
    {
        //frees entire node
        if (node == NULL)
        {
            free(node);
        }

        else
        //iterates over elements of node children
        for (int i = 0; i < N; i++)
        {
            //continues if children[i] points to another node
            if (node->children[i] != NULL)
            {
                //moves down node pointer
                node = node->children[i];

                //calls recursive function
                delete(node);
            }

            else
            {
                continue;
            }

        }

    }
1

There's a problem with program flow in check. This code is never executed!

 //returns true if is_word is also true
            if (cursor->is_word == true)
            {
                return true;
            }

debug50 with a small (e.g. 2 word) text file used as both dictionary and text will help you troubleshoot.

Once that's resolved, you might want to fix the apostrophe "problem". Apostrophes will end up in the z slot, since program indexes the alphabet from 1. The test data (check50) may not detect that, but it is technically incorrect.

2
  • Ah of course, such an obvious oversight. I appreciate you looking over my code. For the apostrophe issue, does it matter what spot in the children array it takes up so long as it is consistent? I"m thinking it should be in exists at [26] and can't think of why that would lead to any errors, but just to be sure. – naobie Dec 17 '19 at 3:35
  • It doesn't matter what spot it takes up as long as it is consistent and unique. Since the alpha index starts at 1, slot 0 is available. Assume the word isn't in the dictionary, isnzt would be "marked" correct. – DinoCoderSaurus Dec 17 '19 at 13:28

You must log in to answer this question.

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