// Implements a dictionary's functionality #include <ctype.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.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; // Number of buckets in hash table const unsigned int N = 26; // Create counter for size int counter = 0; // Hash table node *table[N]; // Returns true if word is in dictionary, else false bool check(const char *word) { // Create node for use in searching through linked list node *cursor = malloc(sizeof(node)); int temp_word = hash(word); cursor = table[temp_word]; while (cursor != NULL) { if (strcasecmp(cursor->word, word) == 0) { return true; free(cursor); } cursor = cursor->next; } free(cursor); return false; } // Hashes word to a number unsigned int hash(const char *word) { // Hash function return toupper(word[0]) - 'A'; } // Loads dictionary into memory, returning true if successful, else false bool load(const char *dictionary) { // Open required file FILE *source = fopen(dictionary, "r"); if (source == NULL) { return false; } // Read each string within the file char buffer[LENGTH + 1]; while (fscanf(source, "%s", buffer) != EOF) { // Create new node and assign read string node *n = malloc(sizeof(node)); if (n == NULL) { return false; } strcpy(n->word, buffer); n->next = NULL; // Carry out hash function int temp_answer = hash(buffer); if (table[temp_answer] == NULL) { table[temp_answer] = n; n->next = NULL; } else { n = table[temp_answer]->next; table[temp_answer] = n; } counter++; } fclose(source); return true; } // Returns number of words in dictionary if loaded, else 0 if not yet loaded unsigned int size(void) { return counter; } // Unloads dictionary from memory, returning true if successful, else false bool unload(void) { // Create nodes to allow freeing memory without disrupting links node *temp = malloc(sizeof(node)); node *cursor = malloc(sizeof(node)); if (temp == NULL && cursor == NULL) { return false; } for (int i = 0; i < N; i++) { cursor = table[i]; temp = cursor; cursor = cursor->next; free(temp); } free(cursor); return true; } Valgrind states "All heap blocks were freed -- no leaks are possible" so that worked okay but when I run check50 I get the following errors:
:) dictionary.c exists
:) speller compiles
:( handles most basic words properly expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:( handles min length (1-char) words expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:( handles max length (45-char) words expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:( handles words with apostrophes properly expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:( spell-checking is case-insensitive expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:( handles substrings properly expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:| program is free of memory errors can't check until a frown turns upside down
Thanks in advance for any advice!!