0

misspelled words in my code is seemed to be 2678 while the correct number is 955 some words like "and", "but", "a", "an" my code regards them as misspelled words i can't find the bug in my code

// Implements a dictionary's functionality
#include <stdbool.h>
#include "dictionary.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <strings.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 = 17576;
int count_word = 0;

// Hash table
node *table[N];

//node *cursor = NULL;
//node *temp = NULL;

FILE *file = NULL;

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

// Hashes word to a number
unsigned int hash(const char *word)
{
    int x = 0, y = 0, z = 0;
    
    //converting the first 3 letters of any word to an int representing number of backet
    if (strlen(word) >= 1)
    {
        if (isalpha(word[0]) != 0)
        {
           x = tolower(word[0]) - 'a';
        }
        else
        {
            x = 0;
        }
    }
    
    if (strlen(word) >= 2)
    {
        if (isalpha(word[1]) != 0)
        {    
           y = tolower(word[1]) - 'a';
        }
        else
        {
            y = 0;
        }
    }
    if (strlen(word) >= 3)
    {
        if(isalpha(word[2]) != 0)
        {    
            z = tolower(word[2]) - 'a';
        }
        else
        {
            z = 0;
        }
    }
    
    
    //returning a valid index
    if((x * 26 + y * 26 + z) > N)
    {
        return ((x * 26 + y * 26 + z) % N);
    }
    else
    {
    return (x * 26 + y * 26 + z);
    }
    
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    char word[LENGTH + 1];
    file = fopen(dictionary,"r");
    unsigned int i;
    
    if (file == NULL)
    {
        return false;
        
    }
    //node *n = NULL;
    
    for (int j = 0 ; j < 17576 ; j++)
    {
        table[j] = NULL;
    }
    
    while (fscanf(file, "%s", word) != EOF)
    {
       node *n = malloc(sizeof(node)); 
       if (n == NULL)
       {
           unload();
           return 1;
       }
       strcpy(n->word , word);
       n->next = NULL;
       i = hash(n->word);
       count_word++;
      if (table[i] == NULL)
      {
          table[i] = n;
      }
      else
      {
          n->next = table[i];
          table [i] = n;
      }
    }
    fclose(file);
    return true;
}

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

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

This is a surprisingly common problem. The error lies here, in the check() function:

while (cursor->next != NULL)

This causes the last word in the linked list NOT to be checked. The last node will have a real word in it, but cursor->next will be null. Since the while test fails, the last node/word is not compared. The while statement should test whether cursor is null, not cursor->next. The more non-empty buckets there are, the worse the problem will be.

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

2
  • that is really solved my problem but these is still a bug – Menna Azban Sep 12 '20 at 21:38
  • That would be for a new question, if you can't solve it. ;-) – Cliff B Sep 12 '20 at 21:42

You must log in to answer this question.

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