0

I am trying to get speller to work. I have not started writing the unload() and size() functions yet. Speller can be compiled but when I run it, it yields the error "dictionary.c:39:13: runtime error: index -97 out of bounds for type 'struct node *[27]'" I think the problem lies in the first "if" loop in the "check" function when I try to loop through the word, but I am not sure how to fix it.

Here is my code:

// Implements a dictionary's functionality

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

#include "dictionary.h"

// Declare the trie data type
typedef struct node
{
bool is_word;
//char word[LENGTH]; // Max length of a word
struct node *children[27]; // 26 alphabets plus apostrophe

}
node;

node *root;


// Returns true if word is in dictionary else false
bool check(const char *word)
{
// TODO


// For each letter in the input word
// Declare the pointer
node *ptr = root;
for (int i = 0; i < sizeof(word); i++)
{
    // Go to corresponding element in the children
    // If NULL, return false
    // Copy the word into a string
    char *word_current = calloc(1, sizeof(word));
    strcpy(word_current, word);
    if (ptr->children[word_current[i] - 'a'] == NULL)
    {
        return false;
        break;
    }
    // If not NULL, move on to the next children
    else
    {
        // Move to the next children
        ptr = ptr->children[word_current[i] - 'a'];
    }

}
// Once at the end of input word
    // Check if is_word is true
if (ptr->is_word)
{
    return true;
}
else
{
    return false;
}
}


// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
// Open the dictionary
FILE *parse_dictionary = fopen(dictionary, "r");
// Check that the dictionary can be opened
if (parse_dictionary == NULL)
{
    return false;
}

// Initialize the pointer
root = calloc(1, sizeof(node));
node *current = root;

// Parse each character from the dictionary
for(int c = fgetc(parse_dictionary); c !=EOF; c = fgetc(parse_dictionary))
{

    // If a word is not a newline character, it belongs to a word
    if (c != '\n')
    {
        // If it is the first letter, set the pointer to the first letter

        // Create node for the letter if the node does not exist
        if (current->children[c - 97] == NULL)
        {
            current->children[c - 97] = calloc(1, sizeof(node));
            current = current->children[c - 97];
        }
        // Otherwise, move to new node and continue
        else
        {
            current = current->children[c - 97];
        }


    }
    // otherwise a word has just ended
    else
    {
        current->is_word = true;
        current = root;
    }
}
fclose(parse_dictionary);

return true;
}

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

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

Thanks for your generous help.

1

sizeof(word) sounds like what you want (here for (int i = 0; i < sizeof(word); i++) {, but it is not. sizeof returns how many bytes the argument uses in memory. Iin this case word is a pointer, and sizeof(apointer) is 4 (32bit machine) or 8 (64bit). Nevertheless you want to know the length of word, so strlen

1
  • Thanks so much for your help @DinoCoderSaurus ! My problem was solved :) – Gladys Lau Dec 17 '18 at 14:53

You must log in to answer this question.

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