-1

I have a dictionary file(containing words only lower case and apostrophe) that is loaded as a trie tree.

I have a check function which checks the words of file if they exist in the trie tree, regardless of letters cases.

Everything works fine except that apostrophe's word are always found as misspelled.

Here is my function

bool check(const char *word)
{
    // creat arrow to follow letters
    NODE* arrow = root;

    // for every letter in word
    for (int i = 0, length = strlen(word); i < length; i++)
    {
        int index;
        if(word[i] == '\'')
        {
           index = 26;
        } else {
              index = tolower(word[i]) - ASCII_DIFFERENCE;
        }

        // if NULL? creat a new one and move arrow to new child
        if (arrow->child[index] == NULL) 
        {
            return false;
        } else if (arrow->child[index] != NULL)  // not null?
        {
            arrow = arrow->child[index];
        } 
    }

    return arrow->isWord;
}  

my struct :

typedef struct NODE
{
    bool isWord;
    struct NODE* child[ALPHA_SIZE];
} NODE;

definitions

#define ASCII_DIFFERENCE 'a'

Any help is appreciated.

Tony Tannous
  • 14,154
  • 10
  • 50
  • 86
Yasser Altamimi
  • 429
  • 5
  • 13
  • Could you provide an example of input and desired output ? and the current output. – Tony Tannous Mar 11 '17 at 22:52
  • sure, the world "i'd" is in the dictionary but the check function still return false and count it as misspelled word @TonyTannous – Yasser Altamimi Mar 11 '17 at 22:54
  • But there is nothing special about the `\'` and I cannot understand why it could create a problem. Are the words correctly inserted into the dictionary? (i.e., do you correctly put it in index 26?) Can you put some print statements in this `if/else` and follow the path in the tree traversal? Also is `ALPHA_SIZE = 27`? – Arash Mar 11 '17 at 23:19
  • Thanks everyone I figured it out, and as always, a stupid mistake, in my load function I didn't load the apostrophes into my tree,, sorry:)) – Yasser Altamimi Mar 11 '17 at 23:33

1 Answers1

0

Apostrophe is hex 27, decimal 39. I think you're using the wrong magic number.

Steve Kolokowsky
  • 423
  • 2
  • 12
  • well, as a decimal ti's 39 but regardless of that I put it into index 26 which is the alphabet size + 1 ,,, so if a-a = 0 and z-a=25 then I want to put apostrophe in 26 – Yasser Altamimi Mar 11 '17 at 23:13