Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Commonmark migration
Source Link

To illustrate what I mean: [This is my dictionary.][1]This is my dictionary. [And this is my output.][2]And this is my output.

Thanks in advance! [1]: https://gyazo.com/29fe77c37231eaa0ccf6e6f3a1137ee2 [2]: https://gyazo.com/51b2441c01e0825212a07a0d27547cfc

To illustrate what I mean: [This is my dictionary.][1] [And this is my output.][2]

Thanks in advance! [1]: https://gyazo.com/29fe77c37231eaa0ccf6e6f3a1137ee2 [2]: https://gyazo.com/51b2441c01e0825212a07a0d27547cfc

To illustrate what I mean: This is my dictionary. And this is my output.

Thanks in advance!

Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Source Link

How can I debug my load() function in Speller?

So I have code that I feel as though should be working, but I'm having trouble checking my work.

I wrote a function that I think loops through my table, but it only displays half of the data seemingly.

So I can't tell if my load() logic is broken, or my printHash() logic is broken.

Any help would be appreciated. Thanks!

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dictionary.h"
void printHash();

#define N 26
// Returns true if word is in dictionary else false

// Main function only so I can run load() without other files
int main(void)
{
    load("dictionaries/small");
}

// Defines the structure of a node
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// Hash function to get index value of a letter.
unsigned int hash(const char *word)
{
    return tolower(word[0]) - 'a';
}

// Keep track of the number of words in the dictionary.
int count = 0;

// Create the hashtable
node *hashtable[N] = {};
// Loads dictionary and stores the words in an alphabetical hashtable.
bool load(const char *dictionary)
{

// Load file, if file is NULL throw error.
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
    printf("Could not load file.");
    return false;
}

// Prints name of dictionary that is being read
printf("%s\n", dictionary);

// Buffer for words
char word[LENGTH + 1];




// Initialize hashtable to null
for (int i = 0; i < N; i++)
{
   hashtable[i] = NULL;
}

// Scan through file one word at a time until end is reached.
while(fscanf(file, "%s", word) != EOF)
{
    // Allocate space for a new node
    node *new_node = calloc(1, sizeof(node));

    // If space not found, throw error.
    if (new_node == NULL)
    {
        //unload();
        free(hashtable);
        free(new_node);
        return false;
    }

    else

    {
        // Copy word from buffer onto node.
        strcpy(new_node->word, word);

        // Set index value for current word
        int bucket = hash(new_node->word);

       // node *temp = hashtable[bucket];

        // Checks if current index is null
        if(hashtable[bucket] == NULL)
        {
            hashtable[bucket] = new_node;
            new_node->next = NULL;
            count++;

        }
    else
        {

            new_node->next = hashtable[bucket];
            hashtable[bucket] = new_node;
            count++;
        }
    }

}
printf("%i\n",count);
printHash();
// Close file
fclose(file);

// Return bool
return true;
}

//Create a node to iterate through hashtable
// Loop through length of hashtable
//     Set node to ith element of hashtable
//     While node is not null
//         Print node value
//         Advance pointer to next item in linked list
void printHash()
{
node *ptr = calloc(1, sizeof(node));
for(int i=0; i < N; i++)
{
    if(hashtable[i] != NULL)
    {
        ptr = hashtable[i];
         while(ptr != NULL)
        {
                printf("%s", hashtable[i]->word);
                printf("\n");
                ptr = ptr->next;

        }
    }
}
}

To illustrate what I mean: [This is my dictionary.][1] [And this is my output.][2]

Thanks in advance! [1]: https://gyazo.com/29fe77c37231eaa0ccf6e6f3a1137ee2 [2]: https://gyazo.com/51b2441c01e0825212a07a0d27547cfc