0

I'm having a lot of trouble with my pset5. I'm attempting to use a trie to load the dictionary. It will compile but nothing really more than that. when using gdb, the program shows "Killed" in the terminal upon running the load function. I've been comparing it to other examples on here and can not make out what the issue is. I really appreciate any help.

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

#include "dictionary.h"

#define APOSTROPHIE 123

//make a trie struct
typedef struct node
{
    bool is_word;
    struct node* children[27];
}
node;

// protype
void cleanup(node* freeer);

// set root for the top level of the trie
node* root; 

// counter for ammount of words in dictionary
unsigned int counter;

// to confirm if dictionary loaded for for the size function
bool loadcheck;


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

    // initializes word counter to 0
    counter = 0;

    // initializes the crawler at the root
    root = calloc(1, sizeof(node));
    node* crawler = root;

    // load dictionary words into memory letter by letter
    for (int c = fgetc(fp); c != EOF; c = fgetc(fp))
    {

        // itterates through the word
        while(c != '\0')
        {

            // assigns apostrophies to children[26]
            if(c == '\'')
                c = APOSTROPHIE;

            // adds each characther to the trie 
            if(crawler->children[c - 'a'] == NULL)
                crawler->children[c - 'a'] = calloc(1, sizeof(node));

            crawler = crawler->children[c - 'a'];
        }

        // end current word
        if (c == '\0')
        {
             crawler->is_word = true;
             crawler = root;
             counter++;
        }
    }

    // end of dictionary
    if(fgetc(fp) == EOF)
    {
        fclose(fp);
        loadcheck = true;
        return true;
    }

}
0

I think I figured it out. My big issue was my while loop, it never returned to the for loop to change the value of c to the next char in the dictionary. This should have been an if statement.

You must log in to answer this question.

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