I am trying to check if I have the basics of the speller program down by first implementing it with linear search. My plan is to get this right then work on a more efficient speller with a trie/hash table.
My dictionary appears to load correctly, and I even checked with printf that the words from the text are being compared to the words in the dictionary.
The problem is all words are being considered "misspelled." Since printf shows the same words being reported, I assumed it must be an error due to comparing arrays, but I thought arrays could be compared since each letter is in an allocated space in memory. Here is my code:
#include <stdbool.h>
#include "dictionary.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
//declare global variables
FILE* bufferdictionary;
int num_of_words = 0;
typedef char string[12+1]; //define string as an array of chars
string dicwords[4]; //note: final program won't know number of words in dictionary
//maybe a struct later?
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char* word)
{
// TODO
for(int i = 0; i<4; i++)
{
printf("Dictionary word is %s and text file words is %s.\n", dicwords[i], word); //error check
if (dicwords[i] == word)
return true;
}
return false;
}
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char* dictionary)
{
// TODO return true if successfully loaded
//open dictionary or return false if can't
bufferdictionary = fopen(dictionary, "r"); //note: dictionary is a char for the command line dictionary declared in speller.c
if (bufferdictionary == NULL)
return false;
//use fscanf to read words and store in buffer - for array have to know number of words, hash table or trie this will happen dynamically (no for loop?)
//when doing hash table or trie will likely need while(!eof(bufferdictionary))
while(num_of_words < 4)
// for(num_of_words = 0; num_of_words < 4; num_of_words++)
{
fscanf(bufferdictionary, "%s", dicwords[num_of_words]);
num_of_words++;
}
return true;
}
/**
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int size(void)
{
// TODO - load already returns 0 if not yet loaded. when tried checking for this, it said load will always be true (because has to make it to this point in the program?)
//check if it's getting correct number of words printf("There are %i words loaded into the dictionary\n", num_of_words + 1);
return num_of_words + 1;
}
/**
* Unloads dictionary from memory. Returns true if successful else false.
*/
bool unload(void)
{
// TODO - won't do this until final program because unloading will be different
fclose(bufferdictionary);
if(bufferdictionary == NULL)
return true;
else
return false; //need to implement code to determine if not freed
}
One note: I first defined a string as an array of LENGTH+1 (max length of a word), but I changed it to 12+1 to match the longest word in my text file to check if the "wasted space" of the array was causing it not to check.
Maybe I'm overcomplicating this by starting with linear search/array comparison, but I thought this would help me understand some of the concepts before complicating with other forms of loading the dictionary.