I've hunted through StackExchange for similar questions and their advice but I'm still struggling to understand why my speller keeps throwing back all the words as misspelled? Would be massively grateful for some guidance please!
// Implements a dictionary's functionality
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "dictionary.h"
// Represents number of children for each node in a trie
#define N 27
// Represents a node in a trie
typedef struct trie
{
bool is_word;
struct trie *children[N];
}
trie;
// Represents a trie
trie *root;
// Create new trie with NULL for all children
trie *createtrie()
{
trie *newtrie = malloc(sizeof(trie));
if (newtrie == NULL)
{
unload();
return false;
}
newtrie->is_word = false;
for (int i = 0; i < N; i++)
{
newtrie->children[i] = NULL;
}
return newtrie;
}
// Indexes char to a number between 0 and 26, inclusive
unsigned int index(char c)
{
if (c == '\'')
{
return 26;
}
return tolower(c) - 'a';
}
// Initialise word counter
int count = 0;
// Free trie and all its children
void unloadtrie(trie *newtrie);
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
// Open dictionary
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
unload();
return false;
}
// Create root TRIE for dictionary
root = createtrie();
// Create traversal trie pointing t root
trie * travtrie = root;
// Buffer for a word
char word[N + 1];
// Insert words into trie
while (fscanf(file, "%s", word) != EOF)
{
// Iterate over each letter in every word
for (int i = 0; i < strlen(word); i++)
{
// Check value at appropriate children
if (travtrie->children[index(word[i])] == NULL)
{
// Create new trie
trie *newtrie = createtrie();
if (newtrie == NULL)
{
unload();
return false;
}
// Point travtrie to newtrie
travtrie->children[index(word[i])] = newtrie;
}
else
{
// Keep iterating through the word
travtrie = travtrie->children[index(word[i])];
}
}
// Word completed
count++;
travtrie->is_word = true;
travtrie = root;
}
// Close dictionary
fclose(file);
// Indicate success
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
return count;
}
// Returns true if word is in dictionary else false
bool check(const char *word)
{
// Create traversal trie pointing to root
trie *travtrie = root;
for (int i = 0; i < strlen(word); i++)
{
// Check if misspelled
if (travtrie->children[index(word[i])] == NULL)
{
return false;
}
else
{
// Continue iterating through the word
travtrie = travtrie->children[index(word[i])];
}
}
printf("checked word is %s\n", word);
// Check word is true
if (travtrie->is_word == true)
{
return true;
}
return false;
}
void unloadtrie(trie *newtrie)
{
for (int i = 0; i < N; i++)
{
if (newtrie->children[i] != NULL)
{
unloadtrie(newtrie->children[i]);
}
}
free(newtrie);
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
unloadtrie(root);
return true;
}