1

My 'Check' function outputs words that are not misspelled and skips over the ones that actually are. It also doubles the words that it outputs. For instance, in a sentence "There was once a catapillar" (with 'catapillar' spelled incorrectly), my function will say that the misspelled words are "ThereThere waswas onceonce aa". I thought that my logic in my code was sound and I even went back through and added comments to represent the pseudocode presented by CS50, but obviously I have done something very wrong. Here is my code for 'Check':

// Implements a dictionary's functionality

#include <stdbool.h>
#include "dictionary.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int word_count = 0;
//array to hold dictionary word
char word[LENGTH + 1];
// Define a trie

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

//create a root
node *root;

//allocate memory for root
root = calloc(1,sizeof(node));

//root points to base children nodes
for (int i = 0; i < 27; i ++){
    root->children[i] = NULL;
}

//point temporary curser at root
node *trav = root;

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    for (int i = 0; i < strlen(word); i++){
    //go to corresponding in trie
        //if NULL, word is misspelled
        //if not NULL, move on to next letter
        while (word[i] != '\0'){
            //if letter is in alphabet
            if (isalpha(word[i])){
                //if node does not exist
                if (trav->children[tolower(word[i]) - 'a'] == NULL){
                    return false;
                }
                //if node does exist
                else{
                    trav = trav->children[tolower(word[i]) - 'a'];
                }
            }
            //if letter is an apostrophe
            else if (word[i] == 26){
                if (trav->children[26] == NULL){
                    return false;
                }
                //if node does exist
                else{
                    trav = trav->children[26];
                }
            }
            //letter must not be in dictionary if not an apostrophe or in alphabet
            else{
                return false;
            }
    //once at end of input word
    if (word[i] == '\0'){
      //check if is_word is true
      return trav->is_word;
    }
    }
    }
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)

//open dictionary
FILE* dict = fopen(dictionary, "r");

//make sure dictionary opens
if (dict== NULL)
{
    fprintf(stderr, "Could not open %s.\n", dictionary);
    return false;
}
fprintf(stderr, "%s opened\n", dictionary);

  //read file until end
while (!feof(dict)){
    fscanf(dict, "%s", word);
    //scanning each letter in word
    for (int i = 0; i < strlen(word); i++){
        char letter = word[i];
        char lowletter = tolower(letter);
        int strnum = strcmp(&letter, "\n");
        //if letter is in alphabet
        if (isalpha(letter)){
        //checking to see if node exists
            if (trav->children[lowletter - 'a'] == NULL){
                //if doesn't exist
                //allocating memory for node
                trav->children[lowletter - 'a'] = (node*) calloc(1,sizeof(node));
            }
            else {
                //points curser to node
                trav = trav->children[lowletter - 'a'];
            }
        }
        //else if apostrophe
        //checking to see if node exists
        else if (trav->children[26] == NULL){
            //if doesn't exist
            //allocating memory for node
            trav->children[26] = (node*) calloc(1,sizeof(node));
        }
        else{
            //points curser to node
            trav = trav->children[26];
        }

        //checking if end of word
        if (strnum == 0){
            //setting bool to true to signify end of word
            trav->is_word = true;
            //moving trav back to root
            trav = root;
            word_count ++;
        }

    }
}
fclose(dict);
return true;
}

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

}

Any and all help/suggestions would be greatly appreciated!

EDIT I have included my updated 'Check' code along with my 'Load' code and 'Size'

3
  • 3
    what's text? why are you opening files in check? You're passed individual words by speller.
    – curiouskiwi
    Nov 6 '18 at 23:01
  • 1
    I was going to ask the same thing. Check is supposed to only check the word passed to it in word when called by main in speller.c. Also, if there's a problem unrelated to this issue, it can just as easily be a problem with load as with check. If you don't resolve this when you recode, please post the load code.
    – Cliff B
    Nov 8 '18 at 4:37
  • 1
    I didn't realize that, thanks! I deleted the code that opens files in 'Check' but the issue still remains. I'll post my 'Load' code along with my newly updated 'Check' code.
    – Tenacity
    Nov 9 '18 at 19:21
1
+50

It looks like I had an extra bracket in my code that hid lots of other simple errors. As soon as I deleted it, they all popped up. I'll have to go through each and every one to make sure my code is bug-free. Hopefully, that will solve my issues!

You must log in to answer this question.

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