0

I have the following code for check. I'm testing it against dictionaries/small and a test.txt and all words are coming back as misspelled? I don't understand why all words are coming back misspelled? Here is what I have for check

    bool check(const char* word)
{
// TODO
char checkWord[strlen(word)];
for(int i = 0, n = strlen(word); i < n; i++)
{
    if(isalpha(word[i]))
    {
        checkWord[i] = tolower(word[i]);
    }
    else
    {
        checkWord[i] = word[i];
    }

}
checkWord[strlen(word)] = '\0';
printf("%s%s", word, checkWord);


int hash = hashFunction(word[0]);
node* curr = table[hash];

while(curr != NULL)
{

    if(strcmp(curr->word, checkWord) == 0)
    {   
        return true;
    }
    curr = curr->next;
}
return false;
}

Here is what I have for load

   bool load(const char* dictionary)
 {
// TODO
FILE* dic = fopen(dictionary, "r");
if (dic == NULL)
{
    printf("error opening dictionary\n");
    return false;
}

char* line = malloc(sizeof(char) * 256);
if (line == NULL)
{
    printf("not enough mem\n");
    return false;
}
int hash;

while(fgets(line, LENGTH + 1, dic) != NULL)
{

    hash = hashFunction(line[0]);
    node* newNode = malloc(sizeof(node));
    if (newNode == NULL)
    {
        printf("not enough space\n");
        return false;
    }
    newNode->next = NULL;
    char* destination = malloc(sizeof(char) * (LENGTH + 1));

    newNode->word = strcpy(destination, line);
    if(table[hash] == NULL)
    {

        table[hash] = newNode;

    }
    else
    {


        newNode->next = table[hash];
        table[hash] = newNode;


    }

}



int n = 0;
for(int i = 0, n = 0; i < BUCKETS; i++)
{
    if(table[i] == NULL)
    {
        n++;
    }

}


if (n == BUCKETS)
{
    return false;
}
else
{
    return true;
}


}
0

For starters, checkWord[] is declared one character too short. strlen() doesn't include the end of string marker \0. So, checkWord[strlen(word)] = '\0'; overwrites whatever is stored immediately after the checkWord string array. Who knows what effect that is having.

If this doesn't resolve your problems, it's possible that the problem lies in load() and not check(). No way to tell without seeing both functions.

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

1
  • I've changed checkWord to be checkWord[strlen(word) + 1] and I still am seeing all words coming back as misspelled. I'm going to add in my load function up top. – LauraK10 Mar 10 '16 at 2:03

You must log in to answer this question.

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