0

bool check(const char *word)

{

int i;

node *loader = root;

for(int c = 0; word[c] != '\0'; c++)

{

    if(isalpha(word[c]))

    {

        i = (tolower(word[c]) % 'a');

    }

    if (word[c] == '\'')

    {

         i = 26;

    }

}

if (loader->children[i] == NULL)

{

     return false;


 }

loader = loader-> children[i];

}

strong text

if (loader->is_word == true)

{

     return true;

}

return false;

}

/ **

  • Loads dictionary into memory. Returns true if successful else false.

*/

bool load(const char *dictionary)

{

root = calloc(1,sizeof(node));

FILE *df = fopen(dictionary, "r");

//check if dictionary open

if (df == NULL)

{

    printf("Can't open dictionary %s.\n" , dictionary);

    return false;

}

node *loader = root;

for (int c = fgetc(df); c != EOF; c = fgetc(df))

{

     if(isalpha(c))

    {

       int i = c - 97;

       if (c == '\'')

       {

          i = 26;

        }

       if(loader -> children[i] == NULL)

       {

           loader -> children[i] = calloc(1,sizeof(node));

           loader = loader -> children[i];

       }


}

else if(c == '\n')

{

    loader -> is_word = true;

    dictsize++;

    loader = root;

   **strong text**}

 }

fclose(df);

return true;

}

I'm not sure how to fix my code. Being trying to figure it out for the whole day, but i'm still can't pass check50 on possessives and substrings. Any suggestions or hints will be useful. Thank you.

1 Answer 1

0

In check, fix your for loop, you probably meant word[c] != '\0', and remove the || word[c] == '\''. You subtract 'a'/97, which is not appropriate for an apostrophe, and since you use an else if instead of an if, the i = 26; will never be executed.

In load, there are various strange things.

  1. You assign a new node if the existing pointer is not NULL, meaning there exists a node already that you now replace.
  2. You advance to the next node only in that case.
  3. Apostrophe is tested only after that part, so if there's one, you got your segfault already.

Instead, it should be different.

  1. You handle apostrophes before touching the children array, assigning i = 26; and not c = 26;
  2. You assign a new node loader->children[i] if the existing pointer is NULL, not is not NULL
  3. Now that you have an existing child node, whether it has existed before or not, you traverse in all cases.
10
  • I fixed the problem that you told me. After fixing it, I can't pass check50 on possessive and substrings still.
    – kickcade
    Commented Mar 17, 2017 at 12:59
  • Can you show your "fixed" code, maybe as an edit to your current question, or as a link to something like github gist, or pastebin?
    – Blauelf
    Commented Mar 17, 2017 at 13:10
  • ok. I edited m code to my code. thank you for your time for the help.
    – kickcade
    Commented Mar 18, 2017 at 1:04
  • loader = loader -> children[i]; should not be within the if
    – Blauelf
    Commented Mar 18, 2017 at 9:23
  • loader = loader -> children[i]; go into the if statement. check50 only passed the one char word. Fail on everything else.
    – kickcade
    Commented Mar 18, 2017 at 9:48

You must log in to answer this question.

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