1

my code is

// Implements a dictionary's functionality

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.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 = 676;

// Hash table
node *table[N];

// size of dictionary
int lines = 0;

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    // TODO
    int a = 0;
    int b = hash(word);
    int c = strlen(word);
    node *cursor = table[b];
    while(cursor != NULL)
    {
        if(strcasecmp(cursor -> word, word) != 0)
        {
            cursor = cursor -> next;
        }
        else
        {
            a = 1;
            break;
        }
    }
    if(a == 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    // TODO
    int a,b,c;
    if(strlen(word) > 1)
    {
        if ('A' <= word[0] && word[0] <= 'Z')
        {
            a = (word[0] % 65);
        }
        else
        {
            a = (word[0] % 97);
        }
        if ('A' <= word[1] && word[1] <= 'Z')
        {
            b = (word[1] % 65);
        }
        else
        {
            b = (word[1] % 97);
        }
        c = (a * 26) + b;
    }
    else
    {
        if ('A' <= word[0] && word[0] <= 'Z')
        {
            a = (word[0] % 65);
        }
        else
        {
            a = (word[0] % 97);
        }
        c = (a * 26);
    }
    return c;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // TODO
    FILE *dictionary1;
    dictionary1 = fopen (dictionary, "r");
    char a[46];
    char b;
    int n = 0;
    int j;
    node *f = NULL;
    if (dictionary1 == NULL)
    {
        return false;
    }
    else
    {
        while((b = fgetc(dictionary1)) != EOF)
        {
            if(b == '\n')
            {
                lines++;
            }
        }
        fseek(dictionary1, 0, SEEK_SET);
        for(int i = 0; i < lines; i++)
        {
            a[0] = fgetc(dictionary1);
            while(a[n] != '\n')
            {
                a[n + 1] = fgetc(dictionary1);
                n++;
            }
            n = 0;
            j = hash(a);
            f = malloc(sizeof(node));
            f -> next = NULL;
            int y =strlen(a);
            for (int k = 0; k < y - 1; k++)
            {
                f -> word[k] = a[k];
            }
            if (i == 0)
            {
                table[j] = f;
            }
            else
            {
                f -> next = table[j];
                table[j] = f;
            }
            f = NULL;
        }
        fclose (dictionary1);
        return true;
    }
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    // TODO
    return lines;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    // TODO
    for (int i = 0; i < 676; i++)
    {
        node *cursor = table[i], *tmp = cursor;
        while(cursor != NULL)
        {
            cursor = cursor -> next;
            free(tmp);
            tmp = cursor;
        }
    }
    return true;
}

it gives

MISSPELLED WORDS

A
cat
is
not
a

for dictionaries/small and

MISSPELLED WORDS

A
cat
is
not
a
caterpillar

for dictionaries/large. I am confused. Please help me.

3
  • In load a is not properly null terminated, and the k loop doesn't iterate over all the chars in a. – DinoCoderSaurus Jun 1 '20 at 11:48
  • What should I do to fix this problem? – Vsjain Jun 1 '20 at 14:17
  • 1
    properly null terminate a and fix the k loop so it iterates over all the chars in a. if a is "cat" then strlen is 3 and a needs 4 bytes and k < y - 1 will finish the loop after 2 bytes. – DinoCoderSaurus Jun 1 '20 at 15:08

You must log in to answer this question.

Browse other questions tagged .