0

The problem is the word is found in loaded dictionary it's found but check function still returns false i've been like 7 days in this problem someone helps please,

((I'm Trying to test code ./speller texts/cat.txt)) ((I'm Trying this in check function to test words loaded from dict and words i get from given text

    char x[]="ca";

    while (table[2] != NULL)
    {
        if (strcmp(table[2]->word,x) == 0)
        {
            printf("caterpillar is found in dictionary \n");
        }
        printf("%s is found \n",table[2]->word);
        table[2] = table[2]->next;
    }

)) i tried to print table[2] which contains all words beginning with letter c and i found cat and caterpillar and i print all words i get from document read and i find cat and caterpillar and still it returns false!!!

// Implements a dictionary's functionality
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include "dictionary.h"

unsigned int sizee;
unsigned int *pointer = &sizee;

struct node* NodeFront(struct node *head, char *data);

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

//The Idea is making an array of 26 pointers to represent alphabetic characters each one will be the head of the linked list of words beginning
//with the same letter like A for example..


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

// Hash table
node *table[N];

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    // Knowing Where to Search in the array of pointers
    int number = hash(word);
    node *temp = table[number];

    // Making all words as a lower case then comparing it with words from dictionary
    int len = strlen(word);
    char word_lower[LENGTH + 1];

    for (int i = 0; i < len; i++)
    {
        word_lower[i] = tolower(word[i]);
    }
    word_lower[len] = '\0';

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

    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
   return (tolower(word[0]) - 97);
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    //open the dictionary to read data
    FILE *file = fopen(dictionary ,"r");

    //if couldn't read dictionary return false
    if (file == NULL)
    {
        printf("couldn't open dictionary \n");
        return false;
    }

    char str[LENGTH + 1];
    unsigned int counter = 0;

    //read a line by line to get each word in the text
    while (fgets(str, LENGTH, file) != NULL)
    {
        int position = hash(str);
        if (table[position] == NULL)
        {
            node *n = malloc(sizeof(struct node));

            //if we couldn't allocate memory for the node
            if (n == NULL)
            {
                unload();
                return false;
            }

            strcpy(n -> word, str);
            n -> next = NULL;
            table[position] = n;
            counter ++;
        }

        else
        {
            table[position] = NodeFront(table[position], str);
            counter ++;
        }
    }
    *pointer = counter;
    fclose(file);
    return true;
}

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

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

struct node* NodeFront(struct node *head, char *data)
{
    node *n = malloc(sizeof(struct node));

    strcpy(n -> word, data);
    n -> next = head;

    return n;
}
0

The problem lies in load. When each line is read, the linefeed char, \n is also being read and written to the end of str.

The code needs to remove that line feed char from the end of each word.

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

2
  • The Problem now is with the case of the word with the 45 characters – Mazen Dec 30 '20 at 12:31
  • Different but related problem. If the read puts 45 chars plus the \n char into word, where is it going to put the end of string marker \0? Is there room for 47 chars???? – Cliff B Dec 30 '20 at 20:35
0

The Fault was that it was extra space after each word that leads to this. I looped at each word and removed that extra space

You must log in to answer this question.

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