I'm struggling with pset 6 (quite difficult logical and implementation wise, but I like this level of difficulty and this challenge). I'm implementing load
and I just found out that I'm 34 words off with the Austinpowers.txt
text.
I have 19224 words, while CS50 reports there are 19190. I know what is setting it off, it's numbers mixed with letters (e.g. '9B'
or '6C'
). The implementation of my code simply checks if the last letter isalpha
(that means once the cursor has reached the end of a word) and it does not double check if the word contains any numbers. I know that there is an easy fix if it's just 2 letters but I am confused as to how I could fix this with words where the number is at the beginning, e.g. '4BDA'
or similar.
How could I fix this issue?
Here is part of my code:
if (isalpha(ch))
{
if (curser->children[tolower(ch) - 'a'] == NULL)
{
curser->children[tolower(ch) - 'a'] = (struct node*)malloc(sizeof(node));
curser->end = false;
}
curser = curser->children[tolower(ch) - 'a'];
curser->previous = ch;
}
else if (ch == '\'')
{
if (curser->children[26] == NULL)
{
curser->children[26] = (struct node*)malloc(sizeof(node));
}
curser = curser->children[26];
curser->previous = ch;
}
else
{
if (isalpha(curser->previous) || curser->previous == '\'')
{
curser->end = true;
totalwords++;
printf("%i\n", totalwords);
curser = root;
}
}
and here is my Trie:
typedef struct node
{
bool end;
struct node* children[27];
int previous;
} node;
Any suggestions and improvements to my code are greatly appreciated! I'm still a novice to coding but I'm trying to get better at it and try to find the best implementation. :)