summaryrefslogtreecommitdiff
path: root/wk5/pset/speller
diff options
context:
space:
mode:
Diffstat (limited to 'wk5/pset/speller')
-rw-r--r--wk5/pset/speller/dictionary.c32
1 files changed, 20 insertions, 12 deletions
diff --git a/wk5/pset/speller/dictionary.c b/wk5/pset/speller/dictionary.c
index 27bbeaa..5722f55 100644
--- a/wk5/pset/speller/dictionary.c
+++ b/wk5/pset/speller/dictionary.c
@@ -22,31 +22,38 @@ node *table[N];
bool check(const char *word)
{
// TODO
+
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
- // TODO: Improve this hash function
- return toupper(word[0]) - 'A';
+ // (sum of (letter - 'A') % 26) of a word to get a value of where to store it in the hash table
+ int val = 0;
+ for (int i = 0; word[i] != '\0'; i++)
+ {
+ val += toupper(word[i]) - 'A';
+ }
+ return val %= 26;
}
-FILE *file = NULL;
+// TODO
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
- // TODO
- file = fopen(dictionary, "r");
- if (file != NULL)
- {
- return true;
- }
- else
- {
- return false;
+ // Open dictionary file
+ FILE *source = fopen(dictionary, "r");
+
+ //read each word in dictionary
+ char c;
+ while (fread(&c, 1, 1, source) != 0) {
+
}
+
+ // Close the dictionary file
+ fclose(source);
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
@@ -54,6 +61,7 @@ unsigned int size(void)
{
// TODO
char c;
+ int n = 0;
while (fread(&c, 1, 1, file))
{
if (c == "\n")