I think I have done everything you're supposed to do in PSET5 Speller. when I manually check the outputs of my code, it works perfectly well. It matches the staff solutions one to one. Yet Check50 fails in all points other than being able to compile the code.
When I check what is wrong it gives me an error similar to the image below for each task. I couldn't decipher the meaning of the error unfortunately. But even in the output I can see that my code correcty identified the misspelled words. It just did not print the benchmarks. Which is done in speller.c which we are not allowed to alter anyway.
I've put the entirety of dictionary.c (my code) below. What am I doing wrong? Why does check50 fail?
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "dictionary.h"
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
const unsigned int N = 26;
node *table[N];
bool check(const char *word)
{
char my_word[LENGTH+1];
for (int i=0;i<LENGTH+1;i++)
{
my_word[i]='\0';
}
for (int i=0;i<strlen(word);i++)
{
my_word[i]=tolower(word[i]);
}
int hash_loc=hash(my_word);
for(node *temp=table[hash_loc];temp!=NULL;temp=temp->next)
{
if(strcmp(temp->word,my_word)==0 && temp->word[0]!='\0')
{
return true;
}
}
return false;
}
unsigned int hash(const char *word)
{
return toupper(word[0]) - 'A';
}
unsigned int my_index=0;
bool load(const char *dictionary)
{
// TODO
FILE *file = fopen(dictionary, "r"); //open the file
if (file != NULL) //check if the file exists
{
char each_word[LENGTH + 1]; //create a variable that will hold each word of the text
while(fscanf(file,"%s",each_word)!=EOF) //loop to scan each word of the text
{
node *new_memory=malloc(sizeof(node)); //create new node
if(new_memory==NULL) //check valid memory
{
return 50;
}
my_index++;
strcpy(new_memory->word,each_word); //copy the scanned text into new node
int hash_loc=hash(each_word);
if(table[hash_loc]!= NULL)
{
new_memory->next=table[hash_loc];
table[hash_loc]=new_memory;
}
else
{
table[hash_loc]=new_memory;
}
}
fclose(file);
return true;
}
return false;
}
unsigned int size(void)
{
return my_index;
}
bool unload()
{
for(int i=0;i<N;i++)
{
for(node* temp=table[i]->next;temp!=NULL;temp=table[i]->next)
{
{
free(table[i]);
table[i]=temp;
}
}
free(table[i]);
}
return true;
}