0

I am still trying to finish the speller pset but without success. I can now get it to compile but its output is incorrect. When I create a smaller dictionary and test text file, with 12 words identical and 1 spelt differently (i.e. so 13 words in total) I get words mispelled:10, words in dictionary:13 and words in text: 12. I have run GDB etc but just cant figure it out. I have spent more time on this pset that the rest combined! Its only stubborness that is keeping me going at this stage. I have set out below the code for my check and load functions. If anyone could give me some guidance on where I need to be looking it would be greatly appreciated, I am stumped!

bool load(const char* dictionary)
{
          //initialise hashtable to NULL
for (int i = 0; i < 26; i++)
{
    hashtable[i] = NULL;
}


// open dictionary file and read it
FILE* dic = fopen(dictionary, "r");

//if empty return false
if (dic == NULL)
{
    printf("Failed to load dictionary.\n");
    return false;
}
else 
{

    //create temp storage for words
    char buffer[LENGTH + 1];
    //continue until end of file reached
    while (fscanf(dic, "%s", buffer) != EOF)
    {
        //build a new node 
        node* new_node = malloc(sizeof(node)); 
        //copy the word from buffer into the new node
        strcpy(new_node->word, buffer);

        count++;

        //put word through hash function to get index for the array
        index = hashy(new_node-> word);

        //if first word at index, put it in and point to Null
        if (hashtable[index] == NULL)
        {
            new_node->next = NULL;
             hashtable[index] = new_node;
        }            
        //if there is already a word at the index, add the new word
        else
        {

                    new_node->next = hashtable[index];
                    hashtable[index] = new_node;
        }
    }
}
   fclose(dic);
   return true;
}

And my check function

bool check(const char* word)
{
//get length of word and store in p
int p = strlen(word);
//set aside memory for array containting word
char worda[LENGTH + 1];

//ensure all lower case letters
for (int i = 0; i < p; i++)
{
        worda[i] = tolower(word[i]);
}

int q = hashy(worda);

if (hashtable[q] == NULL)
{
    return false;
}

node* cursor =  hashtable[q];

while(cursor != NULL)
{
    if(strcmp(cursor->word,worda) == 0)
    {
        return true;
    }
cursor = cursor->next;
}
return false;
}

1 Answer 1

0

The strcpy() function will copy a string from one var to another, starting with the first character and ending with the end of string marker.

So where in your check function do you actually put the end of string marker in worda (before hashing it and comparing to words in the tree)?

There may be other issues, but this will get you going.

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

1
  • Thanks very much Cliff B. Your response got me on the right track. There were other changes to be made which I have done and now it runs correctly and passes check50. So happy to be finished this pset. Hoping the next psets don't get progressively harder or I will be an old man by the time I'm finished the course!
    – Breninio
    Commented Feb 8, 2016 at 22:31

You must log in to answer this question.

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