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

#include "dictionary.h"

//My function declarations
int upper(char letter);
int smaller(int a, int b);
int length(char *word);


int count = 0;
// 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 = 18278;

// Hash table
node *table[N];

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    int index = hash(word);
    node *n = malloc(sizeof(node));
    n = table[index];

    while (n->next != NULL)
    {
        int k = strcasecmp(word, n->word);
        if (k == 0)
        {
            free(n);
            //printf("check\n");
            return true;
        }
    }

    free(n);
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    int l1 = length((char*) word);
    int l2 = 3;
    int l = smaller(l1, l2);
    int total = 0;

    for (int i = 0; i < l; i++)
    {
        int n = upper(word[i]);

        if (n == 0)
        {
            break;
        }

        else if (n < 65 || n > 90)
        {
            l += 1;
            continue;
        }

        else
        {
            total = total * 26 + (n - 65) + 1;
        }
    }

    //printf("hash\n");
    return total - 1;
}

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

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

    while (true)
    {
        node *n = malloc(sizeof(node));
        char *k = malloc(sizeof(45 * sizeof(char)));

        fscanf(file, "%s", k);

        if ((int) k == EOF)
        {
            //printf("load\n");
            break;
        }

        int index = hash(k);
        strcpy(n->word, k);

        if (table[index] == NULL)
        {
            n->next = NULL;

        }

        else
        {
            n->next = table[index];
        }

        table[index] = n;

        count++;
        free(n);
        free(k);
    }

    return false;
}

// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
    //printf("size\n");
    return count;
}

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    // TODO
    return false;
}



//MY FUNCTIONS
int upper(char letter)
{
    if ((int) letter > 96 && (int) letter < 123)
    {
        int k = (int) letter;
        k -= 32;
        return k;
    }

    else
    {
        int k = (int) letter;
        return k;
    }
}

int smaller(int a, int b)
{
    if (a > b)
    {
        return b;
    }
    else
    {
        return a;
    }
}

int length(char *word)
{
    int k = 0;

    for (int i = 0; word[i] != '\0'; i++)
    {
        k++;
    }
    return k;
}

This is my code for pset5/speller (dictionary.c), it seems fine to me and compiles properly as well, but when I try to run it, it always keeps on running without giving any output. I am assuming there is some recursion which keeps on going but couldn't find where the problem is. Please help me out.

1

This if ((int) k == EOF) is a problem in load. EOF will never be "read" into k. EOF is the return value of fscanf. From man fscanf

Return Value

These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.

The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set indicate the error.

2
  • ok but how do i check the return value of fscanf? – Upanshu Lakhani Jan 26 at 17:34
  • set something to it, eg ret_val = fscanf(file,...); – DinoCoderSaurus Jan 26 at 18:09

You must log in to answer this question.

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