0

Running the program, a large portion of text is incorrectly flagged as mis-spelled. I've reviewed & changed the hash / check / load functions but I end up with the same issue. Can someone point me to the issues in my code?

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

// Number of buckets in hash table
const unsigned int N = 676;

// Hash table
node *table[N];

int wordcounter = 0;

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
// TODO

node *list = table[hash(word)];
if (list == NULL)
{
    return false;
}

while(list!=NULL)
{
    if(strcasecmp(word, list->word) == 0)
        {
            return true;
        }
    list = list -> next;
}

return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
// TODO
char copy_word[LENGTH + 1];
strcpy(copy_word, word);
for(int i = 0; i < strlen(word); i++)
{
copy_word[i] = tolower(copy_word[i]);
}

unsigned int hash = 5381;
int c;

for(int i = 0; i < strlen(copy_word); i++)
{
    c = copy_word[i];
    hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}


return hash % N;

}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
// TODO
FILE *f = fopen(dictionary, "r");

char temp_word[LENGTH +1];

//checking if file can be opened
if (f == NULL)
{
    printf("Could not open dictionary.\n");
    return false;
 }

while(fscanf(f, "%s", temp_word)!= EOF)
{
    node *w = malloc(sizeof(w));

    if ( w == NULL)
    {
        return 1;
    }
    strcpy(w -> word, temp_word);
    w -> next = NULL;
    wordcounter++;

    if (table[hash(temp_word)] == NULL)
    {

        table[hash(temp_word)] = w;
    }

    else if(table[hash(temp_word)] != NULL)
    {
        w -> next = table[hash(temp_word)];

        table[hash(temp_word)] = w;
    }
}

//closing file
fclose(f);

return true;
}

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

unsigned int size(void)
{
// TODO

if (wordcounter!=0)
{
    return wordcounter;
}

else
{
    return 0;
}

}

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

 bool unload(void)
 {
// TODO

node *temp;
node *list;

    for(int n = 0; n < N; n++)
    {
        list = table[n];

        while(list!=NULL)
        {
            temp = list -> next;
            free(list);
            list = temp;
        }

    }
return true;

}

1 Answer 1

0

I took a quick peek at the code and saw this:

node *w = malloc(sizeof(w));

This is going to cause problems. Since w is a pointer, it's going to allocate the size of a pointer to the node, not the size of a node. Try this and see if it solves some or all of your problems:

node *w = malloc(sizeof(node));

Don't know if this is the only issue, but it's going to lead to a whole lot of overwritten memory!

2
  • Thanks Cliff! I revised that portion just now, but unfortunately I still have the same amount of errors...
    – Kenny Sun
    Nov 22, 2021 at 0:20
  • Did you actually recompile it? It works fine for me. Also, you didn't change speller.c, right?
    – Cliff B
    Nov 22, 2021 at 22:14

You must log in to answer this question.

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