Skip to main content
2 of 3
Code added

pset5 speller (hash table) - when using large dictionary program prints "mispelled words" and has to be closed with ctrl c

When running speller with the small dictionary and ralph.txt as the text, my program correctly outputs the misspelled words and outputs the statistics (i.e. time in load, number of words in dictionary etc.). It also behaves this way for a large text (i.e. kjv).

I have also tested with a dictionary containing 2572 words and the program manages to produce a correct output for ralph. But for kjv it outputs "MISPELLED WORDS" followed by some words and then has to be ended with ctrl c.

This also happens when I run the program with the large dictionary. However, the only output is "MISPELLED WORDS" for both ralph and kjv and I have to press ctrl c to end the program (see below)

~/workspace/pset5/speller/ $ ./speller dictionaries/large texts/ralph.txt

MISSPELLED WORDS

^C

what could be the problem? Help would be very much appreciated!

(I have completed all four functions and the program passes check50 but given the output problem described above, the program can't be working correctly).

//HASH FUNCTION. hashtable size is 65536

int hash_function(char* needs_hashing)
{
unsigned int hash = 0;
for (int i=0, n=strlen(needs_hashing); i<n; i++)
{
    hash = (hash << 2) ^ tolower(needs_hashing[i]);
}    
return hash % SIZE;
}

//CODE FOR CHECK

bool check(const char *word)
{
 //copy the word from the text into a char array
char word_in_text[LENGTH+1];
strcpy(word_in_text, word);  

//create a pointer for the purposes of traversing the linked list.  
node* cursor = hashtable[hash_function(word_in_text)];

//while the pointer isn't null compare the word with the value of the pointer 
while (cursor != NULL)
{
    //compare word with whatever word cursor is pointing to
    if (strcasecmp(cursor -> word, word_in_text) == 0)
    {
       return true;  
    }
    cursor = cursor -> next; 
    
}
return false; 
}

//CODE FOR LOAD

bool load(const char *dictionary)
{
//open the dictionary 
FILE* inptr = fopen(dictionary, "r");                           
if (inptr == NULL)
{
    unload();
    return false;
}

//loop to read in strings from the dictionary file until its end
while (fscanf(inptr, "%s", dword) != EOF)
{

    //get enough space for the node, check to make sure malloc doesn't point to nothing, then fill in the node 
    node* new_node = malloc(sizeof(node)); 

    if (new_node == NULL) 
    {
        unload();
        return false; 
    }

    strcpy(new_node -> word, dword); 
    new_node -> next = NULL; 
    counter++; 
    
    //find out where to put the word in the hashtable
    int hash = hash_function(new_node->word); 

    //put the newly created node in the hashtable 
    //if there's nothing at that index make the node the first item 
    if (hashtable[hash]== NULL) 
    {
        hashtable[hash]= new_node; 
        
    }
    else //make a linked list 
    {
        new_node -> next = hashtable[hash]; 
        hashtable[hash] -> next = new_node; 
        
    }
}
fclose(inptr);
return true; 
}

//CODE FOR UNLOAD 

bool unload(void)
{

for (int i = 0; i < SIZE; i++)
{
    
    node* traverse_ptr = hashtable[i];  
  
    while (traverse_ptr != NULL) 
    {
        node* delete_me = traverse_ptr;
        traverse_ptr = traverse_ptr -> next;
        free(delete_me); 
    }
}
return true;
}