i have the following problem:
After a few days of working on pset 5, I finally solved it, I tried it with different text and compare the results with the implementation of the staff and the output is the same, but check50 don't think my program is good:
:) dictionary.c, dictionary.h, Makefile exist :( speller compiles \ expected output, not standard error of "dictionary.c:18:5: error: incompatible ..."
I don't understand that error message, so i want to know if somebody knows how to fix this problem.
This is my code for dictionary.c:
/**
* Implements a dictionary's functionality.
*/
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "dictionary.h"
// global variables, the root pf the tria and the size of the dictionary
trie *root = NULL;
unsigned int dictSize = 0;
// prototype of helping functions
int index(char c);
void lowercase(char *word);
bool destroy(trie *node);
trie* createNode(void);
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char *word)
{
// store a lowercase copy of word
char copyWord[LENGTH + 1];
strcpy(copyWord, word);
lowercase(copyWord);
// prepare to navigate throught the trie
int ind;
trie* trans = root;
// go to the specific node where the word could be
for (int i = 0, size = strlen(copyWord); i < size; i++)
{
ind = index(copyWord[i]);
if (trans -> paths[ind] == NULL)
return false;
else
trans = trans -> paths[ind];
}
// check if the word exist in the dictionary's data
if (strcmp(copyWord, trans -> word) != 0)
return false;
else
return true;
}
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char *dictionary)
{
FILE *data = fopen(dictionary, "r");
// return false if the file couldn't be open
if (data == NULL)
{
return false;
}
// point the global root variable to the root node of the trie data structure
root = createNode();
if (root == NULL)
{
return false;
}
// prepare to load the data into the memory
char dictWord[LENGTH + 1];
int ind;
trie* trans = root;
// load each word into the ram memory using a trie data structure
while (fscanf(data, "%s", dictWord) != EOF)
{
for(int i = 0, size = strlen(dictWord); i < size; i++)
{
ind = index(dictWord[i]);
// run through the trie structure creating the neccesary nodes
if (trans -> paths[ind] == NULL)
{
trans -> paths[ind] = createNode();
trans = trans -> paths[ind];
}
else
{
trans = trans -> paths[ind];
}
}
// store the word in a trie node
strcpy(trans -> word, dictWord);
dictSize++;
trans = root;
}
return true;
}
/**
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int size(void)
{
return dictSize;
}
/**
* Unloads dictionary from memory. Returns true if successful else false.
*/
bool unload(void)
{
return destroy(root);
}
/*********************
* HELPING FUNCTIONS *
*********************/
/**
* Return the character equivalent index for an array of 27 elements.
*/
int index(char c)
{
if (c != '\'')
return c - 'a';
else
return 26;
}
/**
* Lowercase each character of a given string.
*/
void lowercase(char *word)
{
for (int i = 0, size = strlen(word); i < size; i++)
{
word[i] = tolower(word[i]);
}
}
/**
* Releases all nodes and sheets that are found under a specific
* node in a trie structure.
*/
bool destroy(trie *node)
{
if (node == NULL)
{
return false;
}
for (int i = 0; i < 27; i++)
{
if (node -> paths[i] != NULL)
{
destroy(node -> paths[i]);
}
}
free(node);
return true;
}
// return a pointer to a new trie node created in the heap
trie* createNode(void)
{
trie* node = malloc(sizeof(trie));
for(int i = 0; i < 27; i++)
{
node -> paths[i] = NULL;
}
return node;
}