0
// Implements a dictionary's functionality

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

#include "dictionary.h"

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node* next;
}
node;

// Number of buckets in hash table
const unsigned int N = 45;

// Hash table
node* table[N];

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    char neword[strlen(word)];
    for(int i = 0; i < neword[i]; i++)
    {
        neword[i] = tolower(word[i]);
    }
    node* item = table[strlen(word) - 1];
    while (item)
    {
        if (strcmp(item -> word, neword))
        {
            return true;
        }
        item = item -> next;
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    return 0;
}

int cnt = 0;

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{

    char* dict = "";
    fread(dict, 1, 999999, fopen(dictionary, "r"));
    if(strcmp(dict, ""))
    {
        return false;
    }
    char splitStrings[9999][45];

    int size = strlen(dict);

    int index = 0;
    for (int i = 0; i < size; i++)
    {
        if(strcmp(&dict[i], "\n"))
        {
            index++;
        }
        else
        {
            strncat(splitStrings[index], &dict[i], 1);
        }
    }

    for (int i = 0; i < cnt; i--)
    {
        node* item = table[strlen(splitStrings[i]) - 1];
        while (true)
        {
            if(!item) break;
            item = item -> next;
        }
        strcpy(item -> word, splitStrings[i]);
        strcpy(item -> word, " ");
    }

    return true;
}

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

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

The actual error is in check() because in the while(item) loop, apperently item is allways null, but i suspect this is because it is not being loaded correctly.

1

this is problematic

fread(dict, 1, 999999, fopen(dictionary, "r"));

you can read one word at a time with fscanf, keep reading it in a loop until fscanf returns EOF.

You must log in to answer this question.

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