So I'm finally done with Week 5 in CS50x and moving in to Week 6(Python).
In the Problem Set for Week 5, We were tasked to build a spell-check application(using C) which spell-checks a file and lists out misspelled words(if any are found).
It might seem like an easy program to make with some other languages and yeah that's true but with C it wasn't so simple because you had to build your own -
- Hash-Table(using Arrays and Linked List)
- A custom Hash Function that hashes the words before they are stored in a Hash Table
- A Linked List which is what holds the words in the Hash Table
And you still had to deal with Memory Management(which can be a pain) by writing a function that allocates and loads the words into memory and also writing a function that unloads(frees) the allocated memory at the end of the program.
But the entire problem turned out to be straightforward if you do it step-by-step. Most of the time i spent was dealing with memory leaks in my custom Hash-Function.
unsigned int hash(const char *word)
{
//THIS HASH FUNCTION IS A MODIFIED VERSION OF THE ONE I GOT FROM THE SECOND ANSWER IN THIS STACK OVERFLOW POST - https://stackoverflow.com/questions/31366098/hash-function-c
int sum = 0;
char cpy[strlen(word) + 1];
strcpy(cpy, word);
for (int i = 0, n = strlen(cpy); i < n; i++)
{
cpy[i] = tolower(cpy[i]);
}
int i = 0;
while (cpy[i] != '\0')
{
if (cpy[i] >= 'a' && cpy[i] < 'a' + 26)
{
sum = sum + (cpy[i] - 'a' + 1);
}
else
{
if (isalpha(cpy[i]))
{
sum += 0;
}
}
i++;
}
return sum;
}
My Hash Function was a modified version of one i got from a stack overflow post.
This problem set wasn't as hard as some previous ones, I spent less than a day on it and now I'm moving to Week 6(Python)..I will post another update when i finish Week 6.
IF YOU'RE INTERESTED IN HARVARD'S CS50X(INTRO TO COMPUTER SCIENCE), ENROLL HERE. IT'S FREE!!!
Top comments (0)