this is my first ask. I ever looked up all the answers and managed well alone but i am really stuck now.
So, i am doing the load and check functions of dictionary. I did a test program with all what i would use in the dictionary itself and when it worked i adjusted it to dictionary, but it is not working.
I tested all the ways i can and now i think the problem is in the load function. I really think my linked list is not working well but i really dont know why. The loop what look for each word, i already did it for all the forms i thought would work (for loops, errors check, etc) and nothing.
Here is my code. Im sure the issue is areally stupid thing but i really cant see.
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "dictionary.h"
#include <string.h>
#define SIZE 26
#define LENGTH 45
typedef struct node
{
char word[LENGTH + 1];
struct node* next;
}
node;
int hash_f(char* key)
{
int n = key[0]-'a';
return n % SIZE;
}
// Define the hashtable
node* hashtable[SIZE];
int count_words = 0;
bool check(const char* word)
{
// copy the word to check
int n = strlen(word);
char n_word[LENGTH + 1];
//descapitallize all word
for (int b = 0; b < n; b++)
{
n_word[b] = tolower(word[b]);
}
n_word[strlen(word) + 1] = '\0';
// Check
int num = hash_f(n_word);
node* src = hashtable[num];
// This tell me that src is ever NULL. fix it.
if(src != NULL)
{
while (src != NULL)
{
if(strcmp(src->word, n_word) == 0)
{
return true;
}
src = src->next;
}
}
return false;
}
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char* dictionary)
{
for (int a = 0; a < SIZE; a++)
{
hashtable[a] = malloc(sizeof(node));
// hashtable[a] = NULL;
}
FILE* file = fopen(dictionary, "r");
if (file == NULL)
{
printf("error, retry\n");
return false;
}
//node* head = NULL;
char c_word[LENGTH];
// read each character in dictionary
int index = 0;
for(int c = fgetc(file); c != EOF; c = fgetc(file))
{
if (isalpha(c) || (c == '\'' && index > 0))
{
// append character to word
c_word[index] = c;
index++;
if (index >= LENGTH)
{
// consume remainder of alphabetical string
while ((c = fgetc(file)) != EOF && isalpha(c));
// prepare for new word
index = 0;
}
}
else if (index > 0)
{
// terminate current word
c_word[index] = '\0';
node* new = malloc(sizeof(node));
strcpy(new->word, c_word);
int num = hash_f(new->word);
if (hashtable[num] == NULL)
{
hashtable[num] = new;
}
else
{
new -> next = hashtable[num] -> next;
hashtable[num] = new;
}
index = 0;
count_words++;
}
}
fclose(file);
return true;
}
One week working in this and i am very depressed. I dont know what i am doing of my life.