My code seems to work fine with all text files. When I look at the errors it doesn't even show actual output, why could that be? https://submit.cs50.io/check50/e963f4484de14f0b73f8fca9a5e9431556cb649a
Here is the code, I would be very grateful for any suggestions as to what is causing the errors in check50.
// Implements a dictionary's functionality
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include "dictionary.h"
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// TODO: Choose number of buckets in hash table
const unsigned int N = 26;
// Hash table
node *table[N];
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
int k = hash(word);
for (node *ptr = table[k]; ptr != NULL; ptr = ptr->next){
char *sa = ptr->word;
int len = strlen(sa);
if(len>0 && sa[len-1]=='\n'){
sa[len-1] = '\0';
}
if(strcasecmp(sa, word)==0){
return true;
}
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
// TODO: Improve this hash function
return toupper(word[0]) - 'A';
}
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
// TODO
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
printf("Could not open dictionary\n");
return 1;
}
int index = 0;
char c, word[LENGTH + 1];
int h;
while (fread(&c, sizeof(char), 1, file)){
word[index] = c;
index++;
if(c == '\n'){
word[index] = '\0';
index = 0;
node *p = malloc(sizeof(node));
strcpy(p->word, word);
p->next = NULL;
h = hash(word);
if(table[h]==NULL){
table[h] = p;
}else{
for (node *ptr = table[h]; ptr != NULL; ptr = ptr->next)
{
// If at end of list
if (ptr->next == NULL)
{
// Append node
ptr->next = p;
break;
}
}
}
}
}
word[index] = '\0';
node *p = malloc(sizeof(node));
strcpy(p->word, word);
p->next = NULL;
h = hash(word);
if(table[h]==NULL){
table[h] = p;
}else{
for (node *ptr = table[h]; ptr != NULL; ptr = ptr->next){
// If at end of list
if (ptr->next == NULL){
// Append node
ptr->next = p;
break;
}
}
}
fclose(file);
return true;
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
int g = 0;
for (int i = 0; i < N; i++){
for (node *ptr = table[i]; ptr != NULL; ptr = ptr->next){
g++;
}
}
return g;
}
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
for (int i = 0; i < N; i++){
node *ptr = table[i];
while (ptr != NULL)
{
node *next = ptr->next;
free(ptr);
ptr = next;
}
}
return true;
}