1

I have tried to implement load/check function as best as I understand it. When I run it, it prints out all the words in the text as misspelled. I don't even know what is wrong with my code at all. Please can you help me figure out what is wrong with my code.`

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "dictionary.h"
#define ALPHABETS 27

typedef struct node
{
    bool is_word;
    struct node* children[ALPHABETS];
}node;
node* root;
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char* word)
{
// TODO
int w_index;

int w_len = strlen(word);
for(int i = 0; i < w_len;i++)
{
    if(isupper(word[i]))
    w_index = (tolower(word[i])) - 'a';
    else
    w_index = word[i] - 'a';
     if(!root->children[w_index])
     {
     root->is_word = false;
     return false;
     }
     else
    root = root->children[w_index];

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

return true;
}

/**
* Loads dictionary into memory.  Returns true if successful else false.
*/
bool load(const char* dictionary)
{
// TODO

char word[LENGTH +1];

node* current = NULL;
current = (node*)malloc(sizeof(node));
if(current)
{
    int i;
    current->is_word = false;
    for(i = 0; i < ALPHABETS; i++)
   current->children[i] = NULL;
}
root = current;
//char c = 'a';
FILE* file = fopen(dictionary, "r");
if (file == NULL)
{
    printf("Could not open %s.\n", dictionary);
    unload();
    return 1;
}
int index;
    while(fscanf(file, "%s", word)!= EOF)
    {
        int length = strlen(word);
       for(int j = 0; j < length; j++)
        {
            index = word[j] - 'a';

                if(!root->children[index])
                    root->children[index] = malloc(sizeof(node));

                root = root->children[index];
        }
        root->is_word = true;
    }

fclose(file);
return true;
}
1

I am going to talk about line numbers based on your code block. For example:

#include <stdbool.h>                   // line #01

current = (node*)malloc(sizeof(node)); // line #57

I see that you are using your root pointer as you should use your current pointer.

The root pointer should not need to change its address at any point in the load function. So you can tell something might trigger errors in line 85:

root = root->children[index];

I suggest you change every mention of root to current and the other way around in your load() function.

Also, I notice you don't have a condition for apostrophes. Whenever you encounter an apostrophe, line 80 is going to cause you some errors:

index = word[j] - 'a';

You might want to add the ternary conditional operator to decide if you want to do your formula (word[j] - 'a') or put the pointer on the last element of the children array (26). It will look something like this:

index    = (word[j] == '\'')? 26   : word[j] - 'a';

variable = (   condition   )? true : false        ;

It means the following:

Is the character in word[j] an apostrophe?

(word[j] == '\'')?

If so, make index equal to 26

else (represented by the colon), make index equal to word[j] - 'a'

: word[j] - 'a';

You must log in to answer this question.

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