I did the speller and had some errors that I solved, but now I'm stuck and need help. I debug the load and hash (I know it's not ideal, but it works:))
Now I'm receiving the Segmentation fault error in function check at the next line
if (strcasecmp(cursor->word, word) == 0)
below you can find the dictionary.c listing
What am I doing wrong?
// Implements a dictionary's functionality
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.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 = 26; // 1 array for each letter A, B, C etc.
// Hash table
node *table[N];
//Number of words in dictionary
int words_num;
// Returns true if word is in dictionary else false
bool check(const char *word)
{
int i, counter;
counter = 0;
node *cursor = NULL;
// TODO
i = hash(word);
while (counter != i)
{
counter++;
}
cursor = table[counter];
while (table[i]->next != NULL)
{
if (strcasecmp(cursor->word, word) == 0)
{
return true;
}
cursor = cursor->next;
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
char array[26];
array[0] = 'a';
array[1] = 'b';
array[2] = 'c';
array[3] = 'd';
array[4] = 'e';
array[5] = 'f';
array[6] = 'g';
array[7] = 'h';
array[8] = 'i';
array[9] = 'j';
array[10] = 'k';
array[11] = 'l';
array[12] = 'm';
array[13] = 'n';
array[14] = 'o';
array[15] = 'p';
array[16] = 'q';
array[17] = 'r';
array[18] = 's';
array[19] = 't';
array[20] = 'u';
array[21] = 'v';
array[22] = 'w';
array[23] = 'x';
array[24] = 'y';
array[25] = 'z';
// TODO
for (int i = 0; i < 27; i++)
{
if (tolower(word[0]) == array[i])
return i;
}
return 0;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
int hash_ind;
node *list = NULL;
node *n = NULL;
node *tmp = NULL;
char *buffer = malloc((LENGTH + 1) * sizeof(char));
words_num = 0;
// Open dictionary file
FILE* file = fopen(dictionary, "r");
if (!file)
{
printf("Unable to open the file");
return false;
}
while (fscanf(file, "%s", buffer) != EOF) // // Read strings from file one at a time. Ends if fscanf retutn EOF
{
n = malloc(sizeof(node));
if (n == NULL)
{
printf("Not enough memory");
return false;
}
strcpy(n->word, buffer);
n->next = NULL;
// Hash word to obtain a hash value
hash_ind = hash(buffer);
// Insert node into hash table at that location
n->next = table[hash_ind];
table[hash_ind] = n;
words_num++;
}
fclose(file);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
// TODO
return words_num;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
node *tmp = NULL;
node *cursor = NULL;
// TODO
for (int i = 0; i < 27; i++)
{
tmp = table[i];
cursor = table[i];
do
{
cursor = cursor->next;
free (tmp);
tmp = cursor;
} while (cursor->next != NULL);
}
return false;
}