0

I have implemented load successfully. When I run speller, words load until the end of file. However, my check function does not work and according to GDB, speller stops when appending chars to word, which is in a for loop, in an if statement ("preparing to spell-check" in speller.c). Maybe because I've been looking at my lines of code for a long time, but I can't figure out what's wrong.

I will post my code below, but please tell me if I should remove it. I do not want solutions, just some hints. Thank you all!

    bool check(const char* word)
   {
    int new_nmb = 0;
    root = malloc(sizeof(node));

   for (int i = 0, n = strlen(word); i < n; i++)
   {
      point = root;


    if (isalpha(word[i]))
    {
        new_nmb = word[tolower(i)] - 'a';

        if (word[i] == '\'')
        {
            new_nmb = 26;
        }

        if (point->children[new_nmb] == NULL)
        {
            return false;
        }
        else
            point = point->children[new_nmb];
    }

    if (word[i] == '\0' && point->is_word == true)
    {
        return true;
    }


  }
   return true;
  }
1

A couple things I see right away...

  1. You malloc space for root here in check. However, you shouldn't need to do that. You probably already did this in load. The logic of check is such that you traverse the trie you created in load to see if the word given as input to this function was a word loaded into your dictionary previously. You are able to do that by holding onto the root node and never losing track of that. Ideally you have root declared in your dictionary.h file, so it's a global variable accessible across your speller.c functions.

  2. The first line of your for loop resets your pointer. A line like this in your code works: point = point->children[new_nmb];, but when your for loop increments, your first step is to reset it back to root, negating anything you just did. You want to set your traversal pointer equal to your global variable root outside this for loop.

****EDIT****

Based on your comment below about not all the words coming through correctly, I took a more detailed look and noticed a couple more things to consider in this bit of code:

if (isalpha(word[i]))
    {
        new_nmb = word[tolower(i)] - 'a';

        if (word[i] == '\'')
        {
            new_nmb = 26;
        }
  1. The second if (if (word[i] == '\'')) will never execute. Your first if asks if it alphabetical or not. The apostrophe will never be alphabetical, and since it's nested, it will never be called. So while it won't break your code, it will prevent words from showing up.
  2. There's a bracketing issue here (new_nmb = word[tolower(i)] - 'a';) with respect to word. Do some math by hand/in your head, and you should see it.

Fix these, and then check should be running just fine.

16
  • When I move my root declaration from the top of the dictionary.c to dictionary.h, I can't compile the program (even though it should be working with make, right?). You are right, I used calloc in load. Now I moved point = root outside of the for loop and ran it with gdb. It seems like now point and root hold the same address constantly now. Thank you for your help. – ThisIsMe Feb 10 '16 at 17:17
  • I would declare it in dictionary.h as a node*, but not malloc or calloc either it or your traversal pointer(s) until the first function that uses them, such as load in the case of root. I hope this helps. If your question is answered, please check it, so the question looks resolved. Thanks! – Peter Feb 10 '16 at 18:07
  • That's what I did, not mallocing in dictionary.h, but it doesn't allow me to declare it there. – ThisIsMe Feb 10 '16 at 18:12
  • You should be able to declare it there, but you just can't initialize it. For example, node* test = value; would fail but node* test; would work just fine. – Peter Feb 10 '16 at 19:26
  • It does now, but speller is still just loading the words and printing out ''Could not unload dictionaries/large." – ThisIsMe Feb 10 '16 at 19:54

You must log in to answer this question.

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