0

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.

0

This if (dicwords[i] == word) doesn't compare the words, it only compares the pointers to those words, and they will always be not equal. If you want to compare two strings, you'll need to use a string function. man string will give you info on them. You may find strcasecmp or strcmp of interest. Pay close attention to what the functions return. (And it ain't boolean!).

1
  • I was thinking arrays acted as a dereferenced pointer (it would compare the values at the value given by the array). I'll look into your suggestions for comprehension and move onto creating my program with dynamic sorting. I found this article helpful on how arrays are treated for anyone interested: eli.thegreenplace.net/2009/10/21/… Aug 8 '16 at 2:16
0

Your check function is incorrect. First of all you are trying to compare a character dicwords[i] to a string word. This will always return false, remember C has no true string data type, you must compare char by char.

The printf() statement in which you check for correctness doesn't tranfer correctly to your loop. Basically each time you call check the first char of dicwords is compared to a pointer to a char word which doesn't contain a character but rather the address in memory where you can find that char.

You need to put more work into check, as of right now it will ALWAYS return false.

1
  • review the declaration of dicwords string dicwords[4];, it's an array of strings. Aug 7 '16 at 19:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .