I wrote check() and then I tried to test speller with dictionary/small and a text file I'd written myself, but the text file couldn't be opened. Is there a problem with the way I implemented load() or check(), or is there something else I'm doing wrong?
This is what the text file looks like:
cata
catterpiller
katerpillar
caterpillar
cat
And here's the code (up to right before unload(), since I haven't implemented that yet - I do need to figure out how to write it as a recursive function and what the base condition has to be):
/**
* dictionary.c
*
* Computer Science 50
* Problem Set 5
*
* Implements a dictionary's functionality.
*/
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "dictionary.h"
/**
* declaration for trie struct
* and length of children array
*/
#define ARRAY_SIZE 27;
typedef struct node
{
bool is_word;
struct node* children[ARRAY_SIZE];
}
node;
/**
* Keeping track of first node of trie here
*/
node* root;
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char* word)
{
for (int index = 0; index < ARRAY_SIZE; index++)
{
if (strcmp((char*)cursor->children[index], word) != 0)
{
return false;
}
}
return true;
}
/**
* variable to hold size of dictionary
*/
unsigned int dict_size = 0;
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
node* new_node;
FILE* dictptr;
bool load(const char* dictionary)
{
dictptr = fopen(dictionary, "r");
if (dictptr == NULL)
{
fclose(dictptr);
exit(1);
}
root = calloc(ARRAY_SIZE, sizeof(node));
if (root == NULL)
{
exit(1);
}
node* cursor = root;
char word[ARRAY_SIZE + 1];
for (int index = 0, n = strlen(word); index < n; index++)
{
new_node = calloc(ARRAY_SIZE, sizeof(node));
if (new_node == NULL)
{
exit(1);
}
fscanf(dictptr, "%s", word);
if (root->children[index] == NULL && root->children[index] == new_node)
{
if (isalpha(word[index]) || word[index] == '\'')
{
cursor = cursor->children[index];
cursor = new_node;
cursor = root;
if (word[index] == '\n')
{
cursor = cursor->children[index];
cursor = new_node;
cursor->is_word = true;
cursor = root;
dict_size++;
}
}
else if (root->children[index] != NULL && root->children[index] == new_node)
{
if (isalpha(word[index]) || word[index] == '\'')
{
cursor = cursor->children[index];
cursor = new_node;
cursor = root;
if (word[index] == '\n')
{
cursor = cursor->children[index];
cursor = new_node;
cursor->is_word = true;
cursor = root;
dict_size++;
}
}
}
}
}
fclose(dictptr);
return true;
}
/**
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int size(void)
{
return dict_size;
}
Any help would be appreciated.
Edit: I just edited the above to update it with the code I have now.