0

I'm working on my spell checker, and everything seems to be compiling. However, when I run the spell checker, the number of words that it finds that are misspelled is way high. I'm not exactly sure why, but I was thinking it could have something to do with the way I converted the ascii values?

if(letter == '\'')
       letter_new = letter - 13;
else
       letter_new = letter - 'a';

Thanks so much!

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

#include "dictionary.h"

#define ALPHABET_SIZE 26

typedef struct node
{
    bool is_word;
    struct node *children[27];
}
node;

node *root = NULL;
int numWords = 0;
bool isLoaded = false;
bool load(const char* dictionary);

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    int letter = 0;
    int length = strlen(word);
    int val = 0;

    root = calloc(1, sizeof(node));
    node *pointer = root;

    for(int i = 0; i < length; i++)
    {
        char c = tolower(word[i]);
        val = (int) c;

        if (c == '\'')
            letter = val - 13;
        else
        {
            printf("%c, %i ", c, val);
            letter = val - 'a';
            printf("\n%i", letter);
        }

        if (!pointer->children[letter])
            return false;
        else
            pointer = pointer->children[letter];
    }
    if (pointer->is_word)
        return true;

    return false;
}

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

    root = calloc(1, sizeof(node));
    node *pointer = root;

    for (char letter = fgetc(dict); letter != EOF; letter = fgetc(dict))
    {
        if (letter == '\n')
        {
            if (pointer != root)
            {
                pointer->is_word = true;
                numWords++;
                pointer = root;
            }
        }
        else
        {
            int letter_new;

            if(letter == '\'')
                letter_new = letter - 13;
            else
                letter_new = letter - 'a';

            if(!pointer->children[letter_new])
            {
                pointer->children[letter_new] = calloc(1, sizeof(node));
                pointer = pointer->children[letter_new];
            }
            else
                pointer = pointer->children[letter_new];
        }
    }

    fclose(dict);
    isLoaded = true;
    return true;
}

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

void clear(node *pointer)
{
    for (int i= 0; i < 27; i++)
    {
        if (pointer->children[i])
        {
            clear(pointer->children[i]);
        }
    }

    free(pointer);
}

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

Do not replace the value of root within check. That way, you replace your trie with an empty one.

Here in Germany we sometimes call that a "Guttenbug", a bug by faulty copy&paste.

(Semi-funny background: Former minister "Guttenberg" had his PhD thesis done with lots of Ctrl+C/Ctrl+V. In my opinion, he paid somebody and did not bother to read it. Neither did the professor. Guttenberg would never admit that part. Currently trying to make his comeback into politics.)

You must log in to answer this question.

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