Well done all in CS50. My pset5 check function keeps listing all words as mispelled. I am using ./speller ~cs50/pset5/texts/austinpowers.txt to test run it. Please point me to where my oversight is. Below is my code, nonetheless not well commented.
CHECK FUNCTION:
bool check(const char* word)
{
// TODO
// determinig length of word received and creating newword to contain corrected upper to lowercase word.
int n = strlen(word);
char newword[n];
for(int i = 0; i < n; i++)
{
if(isalpha(word[i]))
{
if(isupper(word[i]))
{
newword[n] = tolower(word[i]);
}
}
else
{
return false;
}
}
// new node pointer to traverse linked list along derived index from hash function.
node * traverse = buckets[hash(newword)];
// traversing linked list for mispellings.
while(traverse != NULL)
{
if(strcmp(traverse -> word, newword) == 0)
return true;
traverse = traverse -> next;
}
return false;
}
LOAD FUNCTION;
bool load(const char* dictionary)
{
// TODO
// initialising variable to store text scanned by fscanf
char text [LENGTH + 1];
// opening file to read text from
FILE * in = fopen (dictionary, "r");
// checking success of file opening, if not return false for failure.
if ( in == NULL)
{
printf("Failed to load %s.\n", dictionary);
return false;
}
else
{
//while loop to load file and return true if successful
while (fscanf(in ,"%s", text) != EOF)
{
// gets index for hashtable to place read text into from hash function.
int index = hash(text);
// creating nodes to store text. list points to first node and cursor points to nodes added to a list
node * list = (node *) malloc(sizeof(node));
// ensures space for first or additional nodes in heap
if(list == NULL)
{
unload();
return false;
}
//inputs text read from dictionary at start of list or in continuity to list
else
{
// import words into linked list of dictionary words.
strcpy(list -> word, text);
list -> next = buckets[index];
buckets[index] = list;
}
}
// close file and return true for success
fclose(in);
return true;
}
// if file not loaded return false for failure.
return false;
}
HASH FUNCTION;
int hash(const char * string)
{
int index = string [0] - 97;
return index;
}
THESE ARE IN DICTIONARY.H HEADER FILE;
typedef struct node
{
char word[46];
struct node * next;
}node;
node * buckets[26];
// THANKS FOR ALL THE HELP. Salva