0

I am having a problem is pset5 mispellings. My function tells me that all my words are mispelled. Here are my load function an my check function.

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

#include "dictionary.h"

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

unsigned int Size = 0;    


   struct node* root = NULL;

/**
     * Loads dictionary into memory.  Returns true if successful else false.
     */
    bool load(const char* dictionary)
    {
        // open input file
        FILE* fp = fopen(dictionary, "r");
        if (fp == NULL)
        {
            printf("Could not open file.\n");
            return false;
        }
        root = (node*) malloc(sizeof(node));

        bool fresh = false;
        unsigned int c = 0;
        node* cursor = root;

        while(c != EOF)
        {
            c = fgetc(fp);
            if(c != '\n' && c != EOF)
            {
                if(c == '\'')
                {
                    c = 26 + 97;
                }    
                if(cursor->alphabet[c - 97] == NULL)
                {
                    cursor->alphabet[c - 97] = (node*) malloc(sizeof(node));
                    fresh = true;           
                }
                cursor = cursor->alphabet[c - 97];
            }
            if (fresh)
            {
                cursor -> is_a_word = false;
            }
            if(c == '\n')
            {
                cursor->is_a_word = true;
                Size++;
                cursor = root;
            }
            fresh = false;
        }
        fclose(fp);
        return true;
    }


    /**
     * Returns true if word is in dictionary else false.
     */

    bool check(const char* word)
    {
        node* cursor = root;
        unsigned int c =0;
        unsigned int i = 0;

        while(c != '\0')
        {
            c = word[i];

            if(c <= 90 && c >= 65) 
            {
                c |= 32;
            }

            if((c >= 97 && c <= 122) || (c == '\''))
            {
                if(c == '\'')
                {
                    c = 26 + 97;
                }    
                if(cursor->alphabet[c - 97] == NULL)
                {
                    return false;
                }
                else
                {
                    cursor = cursor->alphabet[c - 97];
                }
            }
            if (word[i+1] == '\0')
            {
                break;
            }
            i++;
        }

        return cursor->is_a_word;
    }
2

Simply put, the while loop in check() never starts. Look at the following code:

    unsigned int c =0;
    unsigned int i = 0;

    while(c != '\0')

int 0 is the same as \0 in their binary representation. Because of this, the test condition for the while loop will always be false when it is executed, so none of the code inside of the loop ever executes.

As a side note, in load(), the malloc allocates new nodes, but malloc doesn't initialize the vars in the structure. They will contain whatever garbage data is in memory at that time. You need to either initialize all of the structure elements or use calloc().

Also, the code is setting isword=false as it traverses the trie to the end of the word. This is a problem. If you had already loaded "cat", and then load "cater", it would reset isword for cat to false as it went by.

There may be other issues, but this should get you going.

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

1
  • Thanks a lot Sir for your help ! I appreciate it a lot ! – Taurus Silver Jun 30 '16 at 0:59

You must log in to answer this question.

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