0

I've been having a lot of trouble with Speller. This compiles but almost all the criteria are red, for example it doesn't basic words properly, doesn't handle 1 char words or long words, etc. I'm not quite sure where the problem is, I'm not sure if my load function is ok etc. For the hash function I've cross referenced it with many others who have the same one so I don't think that's it. Code:

// Implements a dictionary's functionality

#include <stdbool.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <strings.h>

#include "dictionary.h"

#include <ctype.h>

#define HASHTABLE_SIZE 10000

// Represents a node in a hash table

typedef struct node

{

char word[LENGTH + 1];

struct node *next;

}

node;

// Hash table

node *table[HASHTABLE_SIZE];

int count = 0;

// Returns true if word is in dictionary else false

bool check(const char *word)

{

unsigned int y = hash(word);

node *cursor = table[y];

while (cursor->next != NULL)

{

if ((strcasecmp(word, cursor->word)) == 0)

{

return true;

}

else

{

cursor = cursor->next;

}

}

return false;

}

// Hashes word to a number

unsigned int hash(const char *word)

{

unsigned int hash_value = 0;

for (int i = 0, n = strlen(word); i < n; i++)

//citation: hash function from Shrey Tripathi on Stackexchange.com

{

hash_value = (hash_value << 2) ^ word[i];

}

return hash_value % HASHTABLE_SIZE;

}

// Loads dictionary into memory, returning true if successful else false

bool load(const char *dictionary)

{

// Opening the dictionary

FILE *file = fopen(dictionary, "r");

if (file == NULL)

{

return false;

}

char word[LENGTH + 1];

while (fscanf(file, "%s", word) != EOF)

{

node *new_node = malloc(sizeof(node));

// Checks if malloc is successful

if (new_node == NULL)

{

unload();

return false;

}

// Copies word into node if malloc succeeds

strcpy(new_node->word, word);

int h = hash(new_node->word);

// Initializes head to point to hashtable index/bucket

node *head = table[h];

if (head == NULL)

{

table[h] = new_node;

count++;

}

else

{

new_node->next = table[h];

table[h] = new_node;

count++;

}

}

fclose(file);

return true;

}

// Returns number of words in dictionary if loaded else 0 if not yet loaded

unsigned int size(void)

{

return count;

}

// Unloads dictionary from memory, returning tru e if successful else false

bool unload(void)

{

for (int j = 0; j < HASHTABLE_SIZE; j++)

{

node *cursor1 = table[j];



while (cursor1 != NULL)

{

node *tempvar = cursor1;

cursor1 = cursor1->next;

free(tempvar);

}

}

return true;

}

1
  • Would you please be so kind as to edit your question and reformat the code? Too many blank lines and no indentation makes it really hard to read and follow.
    – Cliff B
    Nov 5 '20 at 8:09

You must log in to answer this question.

Browse other questions tagged .