0

I used the following functions in speller, but, when I ran the program with the Constitution as text it gave me something like 6900 misspellings out of 7500 words. I don't understand it since I made the load and hash functions in a separate program, and had the cursor move through the hash table and all words were accounted for. Is there a problem with my check function?

Here's my check:

bool check(const char* word)
{       
 //hash word
 hash = hashd(word);

 //make pointer to search with
 node* cursor = malloc(sizeof(node));

 //search through hashtable
 cursor = hashtable[hash];
 while (cursor != NULL)
 {
     if(strcmp( word, cursor->word) == 0)
     {
        return true;
     }
     cursor = cursor->next;
 }
 return false;
}

my load function:

bool load(const char* dictionary)
{

//open file and check for NULL   
FILE* fp = fopen(dictionary, "r");
if (fp == NULL)
{
    printf("Could not open.\n");
    return false;
}   

//initiate hashtable
for (int i = 0; i < hsize; i++)
{
    hashtable[i] = NULL;
}

//fill hashtable
while (!feof(fp))
{
    node* new_node = malloc(sizeof(node));

    fscanf(fp, "%s", new_node-> word);

    //run through hash function
    hash = hashd(word);

    //add new node to table
    new_node-> next = hashtable[hash];
    hashtable[hash] = new_node;
    counter++;
}
return true;

return false;
}

And a really simple hash function:

int hashd (const char* word)
{   

   if (islower(word[0])) 
    {
        first = 3 * (word[0] - 'a');
    }
    if (isupper(word[0]))
    {
        first = 3 * (word[0] - 'A');
    }

    length = strlen(word);

    if (length < 8)
    {
        L = 0;
    }
    else if (length >= 8 && length <= 10)
    {
        L = 1;
    }
    else if (length > 10)
    {
        L = 2;
    }
    hash = L + first; 
    return hash;
}
1

I suspect that you have more than one problem in your code, but can't be sure without seeing all of it. However, I think I located one significant issue. Look at the code near the end of your load function:

while (!feof(fp))
{
    node* new_node = malloc(sizeof(node));

    fscanf(fp, "%s", new_node-> word);

    //run through hash function
    hash = hashd(word);

Shouldn't the last line be hash = hashd(new_node->word); Given what appears to be an excessive use of global variables, I suspect that instead of loading the word that you just read from the dictionary file, you're actually using something in a global var called word. (If so, this is a good demonstration of why the use of globals should be kept to an absolute minimum. If word were not a global, that line would have triggered a compile error.)

As a side note, you have the following at the following at the end of your load() function:

    return true;
    return false;
}

Although it doesn't impact anything, the second return statement is dead code - it will never be executed and should be removed.

If this answers your question, please click the check mark to accept this and remove the question from the unanswered pool. Let's keep up on forum maintenance. ;-)

1
  • Thanks, Cliff B. I changed word to new_node-> when in load and that corrected the bulk of the problem. I also realized that I wasn't converting the text words to lower case before comparing them to dictionary entries, which took care of the rest of the misspellings. Thanks for your point about global variables, too. I'll adjust that and keep it in mind.
    – Warrenwa5
    Sep 13 '15 at 3:21

You must log in to answer this question.

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