I spent a huge amount of time on this pset but I still cannot make the program handle even the basic words properly. When I test the program with "austinpowers.txt", it returns 19190 words mispelled out of 19190 in the text. It returns also an enormous number of words in DICTIONARY as well - 1439228 which makes me think that the problem starts with load and then maybe gets worse somewhere in check. You tell me, here is the relevant code:
At the top:
typedef struct node
{
bool is_word;
struct node* children[27];
}
node;
node* root =NULL;
node* tempPtr = NULL;
int dictionary_size=0;
FILE* dict;
The check function:
bool check(const char* word)
{
int cletter;
int position;
node* crawler = root;
int n;
for (int i = 0; n=strlen(word), i<=n; i++)
{
cletter = word[i];
if(isalpha(cletter)|| cletter=='\'')
{
position = tolower(cletter)-'a';
if (crawler->children[position] == NULL)
{
return false;
break;
}
else if (crawler->children[position] !=NULL)
{
crawler= crawler->children[position];
}
}
else if(cletter=='\0')
{
if(crawler->is_word==true)
{
return true;
break;
}
else if(crawler->is_word != true)
{
return false;
break;
}
}
}
return false;
}
And the load function:
bool load(const char* dictionary)
{
root=(struct node*)malloc(sizeof(node));
for (int i = 0; i < 27; i++)
{
root->children[i]=NULL;
}
tempPtr=(struct node*)malloc(sizeof(node));
tempPtr = root;
for (int j = 0; j<27; j++)
{
tempPtr->children[j]=NULL;
}
tempPtr=root;
dict = fopen(dictionary, "r");
if (dict==NULL)
{
printf("Error, unable to open file\n");
return false;
}
//for every word from the dictionary iterate over the trie
for (int c = fgetc(dict); c != EOF; c = fgetc(dict))
{
// allow only alphabetical characters and apostrophes
if (letter == '\'' || isalpha(letter))
{
letter = tolower(c);
if (tempPtr->children[letter - 'a']==NULL)
{
tempPtr->children[letter-'a']=(struct node*)malloc(sizeof(node));
//then continue
}
tempPtr=tempPtr->children[letter-'a'];
}
//if at the end of the word, set is_word to true
else if(letter=='\0')
{
tempPtr->is_word=true;
dictionary_size++;
tempPtr=root;
}
}
return true;
}
Thanks in advance! I've spent a week on this problem set and once I fix one problem, another pops out. Whenever I change something on the code above, I get segmentation faults so this is the closest I got to making the code compile and run without throwing an error. However, it's still far from working the way it's supposed to.
UPDATE
Ok, so I changed the for-loop and the if-conditions to that:
for (int c = fgetc(dict); c != EOF; c = fgetc(dict))
{
if(c=='\'' || isalpha(c)
{
letter=tolower(c);
//then follows the rest
Not sure this is the best way to do it but it works. In addition, I made some other changes (thanks to @Irene suggestions and many, many hours tweaking with the code) and now the program passes all the requirements in check50. It has some memory leaks but this is a question for another topic :)
As I mentioned above, this problem set took me the longest time so far. I think I must have spent 10% of my time actually writing the code and 90% debugging it. I think it's also because the topic was completely novel to me. It worked well in the end. The general conclusion is that if you spend enough time and efforts on something (like debugging...), it will give the result you were looking for ;)