0

For some reason, when I run check50 cs50/problems/2020/x/speller, it says that my code can't handle substrings, but everything else works. My code recognizes that there are 2 words in the dictionary, but it counts five words as being misspelled rather than four.

// Implements a dictionary's functionality

#include <stdbool.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

#include <strings.h>

#include "dictionary.h"

// Represents a node in a hash table

typedef struct node

{

char word[LENGTH + 1];
struct node *next;

} node;

// Number of buckets in hash table

const unsigned int N = 26;

// Hash table

node *table[N];

int count = 0;

// Returns true if word is in dictionary else false

bool check(const char *word)

{

// TODO
int place = hash(word);
node *c = table[place];

while (strcasecmp(word, c->word) != 0 && c/*->next !*/== NULL)
{
    c = c->next;
}

if (strcasecmp(word, c->word) == 0)
{
    return true;
}

else
{
    return false;
}

}

// Hashes word to a number

unsigned int hash(const char *word)

{

// TODO char alphabet[26];

for (int x = 0; x < 26; x++) //fills solution[] with acii values of the alphabet in capital letters
{
    alphabet[x] = 65 + x;
}

for(int y = 0; y < 26; y++)
{
    if (toupper(word[0]) == alphabet[y])
    {
        return y;
    }
}

return 0;

}

// Loads dictionary into memory, returning true if successful else false

bool load(const char *dictionary)

{

// TODO int index = 0; FILE *file = fopen(dictionary, "r");

if (file == NULL)
{
    return false;
}

char array[LENGTH];

while (fscanf(file, "%s", array) != EOF)
{
    node *n = malloc(sizeof(node));
    
    if (n == NULL)
    {
        return false;
    }
    
    strcpy(n->word, array);
    index = hash(array);
    
    if (table[index] == NULL)
    {
        table[index] = n;
    }
    
    else
    {
        n->next = table[index];
        table[index] = n;
    }
    
    count++;
}

return true;

}

// Returns number of words in dictionary if loaded else 0 if not yet loaded

unsigned int size(void)

{

// TODO
return count;
//return 0;

}

// Unloads dictionary from memory, returning true if successful else false

bool unload(void)

{

// TODO
return true;
node *cursor = table[0];
node *temp = table[0];
for (int x = 0; x < N; x++)
{
    while(cursor != NULL)
    {
    cursor = table[0]->next;
    free(temp);
    }
}

}

0

A problem in check here while (strcasecmp(word, c->word) != 0 && c/*->next !*/== NULL).

If c is NULL, this line will produce a seg fault because there is no c->word. If c is not NULL, control proceeds to the next if, and if word doesn't match, check will return false.

It looks like you have not run any tests and are depending on check50 results to verify the program. While that will be sufficient in a small number of cases, it is not so for this program. The other tests "check" because of the nature of the check50 test data.

Re-think check. If the node is not null, check the word; if the word matches, yay, return true. If the word doesn't match, check the next node.

You must log in to answer this question.

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