0
/**
 * Implements a dictionary's functionality.
 */

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

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

node *root;
node *first;
int count = 0;


/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char *word)
{
    node *initial = first;

    for (int i = 0; i < strlen(word); i++)
    {
        int a = word[i] - 97;

        if (word[i] == '\'')
        {
            a = 26;
        }

        if (initial -> children[a] == NULL)
        {
            initial = first;
            return false;
        }
        else
        {
            initial = initial -> children[a];

        }
    }

    if(initial -> is_word == true)
        {
            return true;
        }

    return false;
}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
bool load(const char *dictionary)
{
    node *root = malloc(sizeof(node));
    for (int i = 0; i < 27; i++)
    {
        root -> children[i] = NULL;
    }

    first = root;

    FILE *fp = fopen(dictionary, "r");
    if (fp == NULL)
    {
        return false;
    }

    while (!feof(fp))
    {
        char c = fgetc(fp);

        if (c == '\'')
        {
            c = 123;
        }

        if (c != '\n')
        {
            if ( root -> children[c-97] == NULL)
            {
                node *new_ = malloc(sizeof(node));
                for (int j = 0; j < 27; j++)
                {
                    new_ -> children[j] = NULL;
                }
                root -> children[c-97] = new_;
                root = new_;
            }
            else
            {
                root = root->children[c-97];
            }
        }
        else
        {
            count ++;
            root -> is_word = true;
            root = first;
        }
    }
    fclose(fp);
    return true;
}

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

/**
 * Unloads dictionary from memory. Returns true if successful else false.
 */
bool unload(void)
{
    return true;
}
2
  • Its not really fine. debug50 can sometimes prevent a seg fault from occuring, but I can't tell you why, even though the underlying problem is still there. Can you provide more detail? What line is triggering the seg fault? Maybe you can add the error messages to the question?
    – Cliff B
    Apr 29 '17 at 16:51
  • The program can be complied properly. But when I execute the program, the result is segmentation fault! Apr 30 '17 at 8:19
0

That's how "unpredictable results" work. The seg fault probably comes here if ( root -> children[c-97] == NULL) because of this while (!feof(fp)). This short around the 7:30 mark has a very specific warning against using feof to detect end of file and alternative approaches.

This

int a = word[i] - 97;

        if (word[i] == '\'')
        {
            a = 26;
        }

is going to cause heartache once the seg fault is sorted out. Apostrophe is ascii 39. if word[i] is an apostrophe, and you subtract 97, it's not an apostrophe anymore.

1
  • Thank you very much! I had just fixed it :) Apr 30 '17 at 8:49

You must log in to answer this question.

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