0

I am currently doing the fifth pset, mispellings. I implemented load correctly via a trie, sturcture, and also size. However the check function is giving me some difficulty.

bool check(const char* word)
{
    node* cursor = root;
  for(int i = 0, n = strlen(word); i < n; i++)
  {
     int compare = 0;
    //converts the letters to numbers
    if(isupper(word[i]))
    {
        compare = word[i] - 'A';        
    }
    else if(islower(word[i]))
    {
        compare = word[i] - 'a';
    }
    else
    {
        //if not letters, then the char must be an apostrophe
        compare = 26;
    }


    if((cursor->children[compare]) != NULL)
    {
        node* temp = cursor->children[compare];
        cursor = temp;

    }
    else
    {
        return false;            
    }   
}
if (cursor->complete)
{
    return true;
}  
else
{
    return false;
}

}

Also attached is the load function, in case there is any problems with it.

/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
bool load(const char* dictionary)
{
root = malloc(sizeof(node));
node* move = root;
FILE* fp = fopen(dictionary, "r");
if (fp == NULL)
{
    return false;
}

//a pointer to keep track of the movement

//loop runs until end of the file
char* word = malloc(sizeof(char) * 46);
while ((fscanf(fp, "%s", word)) != EOF)
{
    move = root;
    for (int i = 0, n = strlen(word); i < n; i++)
    {
        //converts characters to numbers from 0 - 26
        int comp = 0;
        if (isalpha(word[i]))
        {
            comp = word[i] - 'a';
        }
        else
        {
            //if not a letter then it much be apostrophe aka 26
            comp = 26;
        }


        if (move->children[comp] == NULL) 
        {
            if((n - i) == 1)
            {
                move->children[comp] = malloc(sizeof(node));
                move->complete = true;  
            }
            else
            {
                move->children[comp] = malloc(sizeof(node));
                node* temp = move->children[comp];
                move = temp;
            }
        }

        else
        {
            if((n - i) == 1)
            {
                move->complete = true;  
            }
            else
            {
                node* temp = move->children[comp];
                move = temp;
            }
        }         

    } 
    count++;

}

  return true;
 }

Upon comparing my answers to the staff solution, my check seems to detect much more misspelled words than the staff solution.

All help would be appreciated, as I am having a brain freeze right now and can't see the issue. Thank!

My node struct

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

gdb should give you an idea of what's going on. I'll give you some hints though:

in your load function, you have this:

else
{
    if((n - i) == 1)
    {
        move->complete = true;  
    }
    else
    {
        node* temp = move->children[comp];
        move = temp;
    }
}

in the if branch, are you sure you wanna set move->complete to true? are you sure move represents the character you want here?

also note that fscanf won't object at all reading a string longer than 45 characters causing you troubles probably.

by the eye, I don't think there is anything wrong with check.

as a side note, you could update move without an additional variable.

1
  • I genuinely can't thank you enough, I realized I was marking the bool wrong and came up with that, but didn't see the issue with it. It's all matching the staff solution!! Dec 28 '15 at 20:32

You must log in to answer this question.

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