0

These are my codes for the check and load functions.

bool check(const char* word)
{
// TODO
node* trav=root;
char c;
//char s[100];
//strcpy(s,word);
for(int i=0;i<strlen(word);i++)
{c=word[i];
    if(isalpha(c))
    {
    if(isupper(c))    
     c=tolower(c);
     if(trav->children[c-97]!=NULL)
     trav=trav->children[c-97];

     else
     {
     //free(trav);
     return false;
     }

     }

else if(c=='\'') //change here
{if(trav->children[26]!=NULL)
 trav=trav->children[26];
 else
 return false;
}

else
{//free(trav);
return false;

}
}

if(trav->is_word==true)
{   //free(trav);
    return true;
}
else
{//free(trav);
return false;}
}

bool load(const char* dictionary)
{
// TODO

char c;


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

c=fgetc(stream);
root=malloc(sizeof(node));
for(int i=0;i<27;i++)
root->children[i]=NULL;//changed here
node* prev=root;
while(!feof(stream))
{


if(c!='\n'&&c!=EOF)
{   
    if(isalpha(c))
    {
    if( prev->children[c-97]==NULL)
    prev->children[c-97]=malloc(sizeof(node));
    prev=prev->children[c-97];}
    else if (c=='\'')//change here
    {prev->children[26]=malloc(sizeof(node));
    prev=prev->children[26];}
    //prev=prev->children[c-97];
}
else if(c=='\n')
{prev->is_word=true;
prev=root;
dwords++;    
}
c=fgetc(stream);
}
//free(prev);//change this if it does not work
fclose(stream);
return true;

}

It works for a dictionary with just one word it, but outputs 'segmentation fault' in all other cases. Can you tell me where exactly I am going wrong?

EDIT: I have subsequently got rid of the segmentation fault but when I compare my spell check of austinpowers.txt with the staff solution, there seems to be a difference in the number of misspelt words, in particular some of the "apostrophe'd" words like I'm and I'll (some others like EVIL'S are working fine in my code). I have updated my load function and also pasted my check function.

0
prev=root->children[c-97];

What if c is '\n'? What is '\n' - 97? Can you have a negative array index?

2
  • Hey thanks for your help! I have updated my code and the segmentation fault does not show up anymore. There is another error however. Can you please check out my updated code (and edit) and suggest where the problem may be?
    – user14554
    Dec 30 '16 at 14:45
  • @user14554 Your code generates different indexes for table if it's a punctuation, lowerCase letters. You have to santize this in load and check functions
    – C--
    Apr 14 at 17:25

You must log in to answer this question.

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