0

I'm having trouble with my load function because it seems that the is_word bools are and aren't being set like they should be (as I can see when I go through it with GDB). Some of them are being set but others aren't. Can you help?

edit: here is the check function

  bool check(const char* word)
  {

      //create copy of the input word(for manipulation)
      char* copy = malloc(sizeof(*word));

      memcpy(copy, word, strlen(word)+1);


    //initialize the cursor to the top of the main data structure
    cursor = root;

    //iterate trhought the word
    for (int i = 0, n = strlen(copy); i < n; i++)
    {   
        //order within the node structure
        int Ord;

       //for ith char in char*, have ptr check if cursor->children[the right one] == NULL
        if (isalpha(copy[i]) || (copy[i] == '\'' && i > 0))
        {
            //convert all letters to lower(for simplicity)

            if (isupper(copy[i]))
            {
                copy[i] = tolower(copy[i]);
            }

            //store apostrophes as position 26
            if (copy[i] == '\'')
            {
                Ord = 26;
            }

            //else order is based on ascii
            else
            {
                Ord = copy[i] - 'a';
            }

        } 

        //not a valid char, thus false
        else
        {
        break;    
        }

        //check if current pointer has a valid ptr at the position in question
        if (cursor -> children[Ord] == NULL)
        {
            //if the bool is true, it's the last char in a word
            if (cursor -> is_word == true)
            {
                free(copy);

                return true;
            }

            //otherwise it is not a valid word
            else
            {
                free(copy);

                return false;
            }
        }

        else if( i + 1 == strlen(copy) && cursor -> is_word == true)
        {
            free(copy);
            return true;
        }

        else
        {
            //move down a level by reseting the cursor
           cursor  = cursor -> children[Ord]; 
        }


    }

    free(copy);

    return false;
  }
4
  • I ran your code through my version of dictionary.c I had to make a slight alteration to get it to compile - changed else if (word[i] == '\'') to a simple else clause. Once I did this, the code ran fine and passed check50. Is it possible that the problem lies in the check() function?
    – Cliff B
    Aug 14 '16 at 0:51
  • hm weird. I focused on the load function because when I went through it with gdb, the problem seemed to be that cursor -> is_word was returning false seemingly randomly. For example the fourth word in texts/quote.txt is "that," but the bool isn't showing as true.
    – cb3k
    Aug 14 '16 at 5:56
  • just posted check
    – cb3k
    Aug 14 '16 at 6:00
  • Are you absolutely sure? Is it possible that you were looking at it before it was set? GDB displays the line of code that is about to be executed, not the one that just executed.
    – Cliff B
    Aug 14 '16 at 6:00
1

The check function has some serious logic issues. As written, among other issues, it is depending on finding a null child to detect the end of a word in one case. This could produce a false positive. Say that dog is in the dictionary and doggy was not. As written, the code would find that the child representing the second g is null and would see the is_word==true for dog and give a false positive when doggy isn't in the dictionary.

There are other issues, because the code is finding every word misspelled, even with a 1 word dictionary. On each pass, it went to the very last else clause for each letter and then fell through to the default return false at the end. (I'm not prepared to spend the time to completely diagnose the logic in this code, just not up to it right now.)

The check function needs to walk through the trie to the end of the word before it looks at is_word. If it can't walk to the end of the word, then it's not in the dictionary and should return false. Only when it gets to the end should it check for is_Word == true.

Also, you can simplify some of the code easily. For example, it is not necessary to check isupper() before using tolower(). If a char is anything other than an uppercase letter, it will simply return whatever the character is, unchanged. You could do Ord = islower(copy[i]) - 'a'; and save several lines of code.

My suggestion would be to rewrite your check() code and clean up the traversal of the trie before checking for is_word.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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