0

The 'speller' program considers any word with more than 7 characters as misspelled, even though that word is there in the dictionary. Same treatment is given to any word which comes first in the file, again, even though present in the dictionary.

I take every word in the text and convert them into lowercase using : enter image description here

During this process in debugging mode I have observed that any ("low") word's value is Oxecc8dO by default. But, two peculiar scenarios exist:

1) The first word: It will have additional "220\362\036\037\377\177" suffixed to Oxecc8dO initially. enter image description here

But, when the first letter gets converted to lower, it replaces "220" in "220\362\036\037\377\177". enter image description here

2) Word with more than 7 characters: Initially it will just be Oxecc8dO, but, the moment 8th character gets in, "220\362\036\037\377\177" gets suffixed. For ex: for a word 'intentional', the moment 'o' gets in, the value changes from Oxecc8dO to Oxecc8dO"intentio\220\362\036\037\377\177".

enter image description here

Could these be the reasons for the program to consider them as misspelled words. Kindly help as this is the only thing standing between me and the CS50 certificate, that too since months.

Below i have provided the relevant code:

  • define LENGTH 45

  • Definition of a node

enter image description here

  • Checking the word's spelling enter image description here

bool check(const char *word)

{ unsigned int y = hash(word);

char *low = malloc(sizeof(word) + 1);
if (!low)
{
    return false;
}

for (int i = 0; i < strlen(word); i++)
{
    low[i] = tolower(word[i]);
}

for (node *ptr = hashtable[y]; ptr != NULL; ptr = ptr -> next)
{
    if (strcmp(ptr -> word, low) == 0)
    {
        free(low);
        return true;
    }
}
// TODO
free(low);

return false;

}

4
  • Can you please add the declaration of low to the question? Mar 17 '20 at 12:10
  • There are any number of possibles here. Without seeing code, it's just guessing, but here are some guesses. First, if words are truncating at 7 letters, and you add in the end of string marker, that's 8 bytes, a common length of certain data types. Bottom line is that we need to see all the relevant code for this.
    – Cliff B
    Mar 17 '20 at 23:12
  • @CliffB: Thank you for your response. My doubt is why would the words truncate at 7 letters. According to me there isnt any code which has limited the size to just 7. I have changed my question above to include the relevant code. Thanks in advance. Mar 18 '20 at 4:41
  • @DinoCoderSaurus: Thank you for responding to my query. I have changed my original post to include the relevant code. Kindly have a look. Mar 18 '20 at 4:41
0
malloc(sizeof(word) +1);

I thought it would be something like this. You are allocating 9 bytes. Here's why. A call to sizeof returns the size of an object. The object here is word, a pointer. ALL pointers are 8 bytes. The code adds 1 to it and there are 9 bytes. When the word is copied in, the results are unpredictable at best. sizeof() looks at the pointer's size, not the size of what it points at.

Try using strlen() instead of sizeof. Be sure to read the man pages for both, to understand how they work.

No guarantee that this solves your problem, but it'll probably take a big chunk out of it.

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

5
  • thank you for your attempt in solving my problem. But, even after I replaced 'sizeof' with 'strlen', there was no difference seen. Sorry Mar 18 '20 at 7:41
  • Then maybe it's time to post the code for load to see if there's a similar problem there.
    – Cliff B
    Mar 18 '20 at 16:38
  • I am sorry that i am a novice in using these sites for help. Can you please tell me what exactly you want me to do. Mar 19 '20 at 3:49
  • Edit your post. Delete the screenshot of the code. Post your code using copy and paste so that others can copy it out to test without having to transcribe the entire program.
    – Cliff B
    Mar 19 '20 at 6:00
  • thanks a lot for your prompt responses Mar 19 '20 at 15:22

You must log in to answer this question.

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