0

My code passes all the check50 checks, apart from a small amount of memory leak (I think I'm unnecessarily allocating memory to my temporary nodes).

When I run the small dictionary with cat.txt, it shows me that everything is as it should be ie. 'cat' and 'caterpillar' are in the dictionary but the rest of the words in the file show up as 'misspelled'.

However, when I run the large dictionary with any of the texts, it tells me there are 0 words misspelled. Here is a side by side comparison of my stats and the staff stats, from the 'Lalaland' text:

enter image description here

I think the problem is in my 'Check' function because the dictionary seems to be loading fine. Here is my code for the check function:

bool check(const char *word)
//Convert word to lower case
{
char tmp_word[strlen(word)];
strcpy(tmp_word, word);
for (int i = 0; tmp_word[i] != '\0'; i++ )
{
    tmp_word[i] = tolower(word[i]);
}

//Hash word to get hash value
int hash_value = hash(tmp_word);

//Create traversal pointer
node *trav = malloc(sizeof(node));
if (trav == NULL)
{
    return 2;
    printf("Not enough memory\n");
}

//Assign values in trav, point traversal pointer to head of list
strcpy(trav->word, word);
trav->next = table[hash_value];

//Check to see if the word in the list is the same as the one in the trav node
while (trav->next != NULL)
{
    if (strcasecmp(table[hash_value]->word, trav->word) == 0)
    {
        return true;
    }
    else
    {
        trav = trav->next;
    }
}
free(trav);
return false;
}

Can anyone give me any tips? I've tried debugging but can't work out what's going wrong!

1

The problem is here if (strcasecmp(table[hash_value]->word, trav->word) == 0). table[hash_value]->word never changes in the loop. Matches will only be detected when the text word matches the last word in the hash_value index list.

1
  • Thanks!! I changed some things around and fixed the code :) – discopi Dec 17 '20 at 12:12

You must log in to answer this question.

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