// 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.