0

please take a look of my code and offer me some suggestion on why my code returning all words as misspelled. thank you so much.

// Implements a dictionary's functionality
#include<stdio.h>
#include <stdbool.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include "dictionary.h"

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

node *root;
int wordcount = 0;
char c;

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    node *cursor = root;
    int j;
    for (int i = 0; i <= strlen(word); i++)
    {
        if (word[i] == '\'')
        {
            j = 26;
            if ( cursor->children[j] == NULL )
                return false;
            cursor = cursor->children[j];
        }
        else if(isalpha(word[i]))
        {
            j = toupper(word[i]) - 'A';
            if ( cursor->children[j] == NULL )
                return false;
            cursor = cursor->children[j];
        }

        if ( cursor->is_word == true )
            return true;
    }
    return false;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    root = calloc(1, sizeof(node));
    FILE *masterfile = fopen(dictionary, "r");
    if (masterfile == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", dictionary);
        return false;
    }

    for (c = fgetc(masterfile); c != EOF; c = fgetc(masterfile))
    {
        int i;
        node *cursor = root;

        if (c != '\n')
        {
            if (c == '\'')
            {
                i = 26;
                if ( cursor->children[i] == NULL )
                {
                    cursor->children[i] = calloc(1, sizeof(node));
                }
                cursor = cursor->children[i];
            }
            else if(isalpha(c))
            {
                i = c - 'a';
                if ( cursor->children[i] == NULL )
                {
                    cursor->children[i] = calloc(1, sizeof(node));
                }
                cursor = cursor->children[i];
            }
        }
        else
        {
            cursor->is_word = true;
            wordcount++;
            cursor = root;
        }
    }
    return true;
}
2
  • Can you add the load code and the node struct declaration to the question please? – Cliff B Aug 1 '18 at 2:59
  • Please see updated code. thank you – Issa Aug 1 '18 at 3:03
0

As expected, the problem lies inside load, not check.

What happens when cursor is reinitialized inside the for loop for every character on every loop pass?

Side note - which is more appropriate for something that has to loop based on a condition, a for loop or a while loop?

BTW, there's another problem lying in wait, but that'll be a new question. ;-)

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

7
  • it returns the correct number of words in dictionary so I assumed the load function works... Could you please explain the problem in more details? isn't the for loop loading characters from dictionary to trie? – Issa Aug 1 '18 at 4:02
  • Yes, the loop is for that, but it has to progress from one node to the next to form a linked list. As written, the cursor is being reset back to root for EVERY letter, so no word will ever go past 1 letter and every letter produces a node off of root.. – Cliff B Aug 1 '18 at 4:07
  • ... and that's a good lesson. Just because a program "runs to completion" during development, it rarely means that it did it correctly! It's necessary to test with specific data and verify the correctness of the output, and even that you got the right results for the right reason. Ever hear of false positives? ;-) – Cliff B Aug 1 '18 at 4:29
  • thank you so much!! I am finally done with speller. cannot believe i struggled three days just because I did not notice this simple mistake! – Issa Aug 2 '18 at 4:14
  • Hi I have a side question. When I was working on this, I always wondered why checking function is before loading function. Isn't it we have to load the dictionary first then check the words in dictionary? or the order of functions does not matter? – Issa Aug 3 '18 at 2:37

You must log in to answer this question.

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