Struggling with the load function in speller for second week now. What I found using debug50 and placing "printf" between different lines of code is that the fscanf reads every second word in the dictionary. The counter that is supposed to sum the amount of words prints 4 instead of 7 that is actually in dictionary. Any thoughts what might be the problem here?
// Implements a dictionary's functionality
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <ctype.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 = 25;
// Hash table
node *table[N];
//declare global counter to keep track of words loaded
int counter = 0;
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
//hash word to see it's hash value
//access linked list at that index
//traverse linked list using strcasecmp
return 0;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
unsigned int i;
if (islower(word[0]))
i = word[0] - 97;
else
i = word[0] - 65;
// TODO
return i;
}
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
// TODO
//open dictionary file
FILE *input = fopen(dictionary, "r");
if (input == NULL)
{
return false;
}
//read stringsfrom file one at a time
char buffer[LENGTH + 1];
while (fscanf(input, "%s", buffer) != EOF)
{
fscanf (input, "%s", buffer);
//create new node for each word
node *n = malloc(sizeof(node));
strcpy(n->word, buffer);
n->next = NULL;
//hash word to obtain a hash value
int a = hash(n->word);
//insert node into hash table at that location
if (table[a] == NULL)
{
table[a] = n;
counter++;
}
else
{
n->next = table[a];
table[a] = n;
counter++;
}
}
fclose(input);
return true;
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
// TODO
printf("%i\n", counter);
return counter;
}
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
// TODO
return false;
}