Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Implements a dictionary's functionality.
- */
- #include <stdbool.h>
- #include <stdio.h>
- #include "dictionary.h"
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- int index = 0;
- typedef struct node
- {
- char word[LENGTH + 1];
- struct node *next;
- }
- node;
- node *hashtable[26];
- /**
- * Returns true if word is in dictionary else false.
- */
- bool check(const char *word)
- {
- // TODO
- /*
- hash the word
- then, with the hashed function, search the desired hashtable for the word using string compare strcmp through linked list
- 1. hash word
- 2. search through linked list (loop) and strcmp every node
- 3. if not found till end, then return false
- */
- int len = strlen(word);
- char word_copy[LENGTH + 1];
- // convert word to lowercase and store it in word_copy
- for (int i = 0; i < len; i++)
- {
- word_copy[i] = tolower(word[i]);
- }
- // add null terminator to end of char array
- word_copy[len] = '\0';
- int hashy;
- if (word_copy[0] == 'a')
- {
- hashy= 0;
- }
- else if (word_copy[0] == 'b')
- {
- hashy=1;
- }
- else if (word_copy[0] == 'c')
- {
- hashy=2;
- }
- else if (word_copy[0] == 'd')
- {
- hashy=3;
- }
- else if (word_copy[0] == 'e')
- {
- hashy=4;
- }
- else if (word_copy[0] == 'f')
- {
- hashy=5;
- }
- else if (word_copy[0] == 'g')
- {
- hashy=6;
- }
- else if (word_copy[0] == 'h')
- {
- hashy=7;
- }
- else if (word_copy[0] == 'i')
- {
- hashy=8;
- }
- else if (word_copy[0] == 'j')
- {
- hashy=9;
- }
- else if (word_copy[0] == 'k' )
- {
- hashy=10;
- }
- else if (word_copy[0] == 'l')
- {
- hashy=11;
- }
- else if (word_copy[0] == 'm')
- {
- hashy=12;
- }
- else if (word_copy[0] == 'n')
- {
- hashy=13;
- }
- else if (word_copy[0] == 'o')
- {
- hashy=14;
- }
- else if (word_copy[0] == 'p')
- {
- hashy=15;
- }
- else if (word_copy[0] == 'q')
- {
- hashy=16;
- }
- else if (word_copy[0] == 'r')
- {
- hashy=17;
- }
- else if (word_copy[0] == 's')
- {
- hashy=18;
- }
- else if (word_copy[0] == 't')
- {
- hashy=19;
- }
- else if (word_copy[0] == 'u')
- {
- hashy=20;
- }
- else if (word_copy[0] == 'v')
- {
- hashy=21;
- }
- else if (word_copy[0] == 'w')
- {
- hashy=22;
- }
- else if (word_copy[0] == 'x')
- {
- hashy=23;
- }
- else if (word_copy[0] == 'y')
- {
- hashy=24;
- }
- else if (word_copy[0] == 'z')
- {
- hashy=25;
- }
- else
- {
- return false;
- }
- node* cursor = hashtable[hashy];
- while (cursor != NULL)
- {
- if (strcmp(word_copy, cursor->word) == 0)
- {
- // word is in dictionary
- return true;
- }
- // check next node
- cursor = cursor->next;
- }
- return false;
- }
- /**
- * Loads dictionary into memory. Returns true if successful else false.
- */
- bool load(const char *dictionary)
- {
- // TODO
- //Hashtables
- //sort dictionary into alphabetic order. a - z
- FILE *file = fopen(dictionary, "r");
- if (file == NULL)
- {
- printf("Failed to load dictionary");
- return false;
- }
- //25 means 0-24
- for (int i = 0; i < 26; i++)
- {
- hashtable[i] = NULL;
- hashtable[i] = malloc(sizeof(node));
- hashtable[i]-> next = NULL;
- }
- // malloc hashtable
- char word[LENGTH + 1];
- while (fscanf(file, "%s", word) != EOF)
- {
- /* if word starts with "a" then put it in node for hashtable[0]
- else if word starts with "b" then put it in node for hashtable[1]
- else if word starts with "c" then put it in node for hashtable[2]
- else if word starts with "d" then put it in node for hashtable[3]
- else if word starts with "e" then put it in node for hashtable[4]
- else if word starts with "f" then put it in node for hashtable[5]
- else if word starts with "g" then put it in node for hashtable[6]
- else if word starts with "h" then put it in node for hashtable[7]
- else if word starts with "i" then put it in node for hashtable[8]
- else if word starts with "j" then put it in node for hashtable[9]
- else if word starts with "k" then put it in node for hashtable[10]
- else if word starts with "l" then put it in node for hashtable[11]
- else if word starts with "z" then put it in node for hashtable[25]
- */
- if (word[0] == 'a' || word[0] == 'A')
- {
- strcpy(hashtable[0]-> word, word);
- index++;
- }
- else if (word[0] == 'b' || word[0] == 'B' )
- {
- strcpy(hashtable[1]-> word, word);
- index++;
- }
- else if (word[0] == 'c' || word[0] == 'C' )
- {
- strcpy(hashtable[2]-> word, word);
- index++;
- }
- else if (word[0] == 'd' || word[0] == 'D' )
- {
- strcpy(hashtable[3]-> word, word);
- index++;
- }
- else if (word[0] == 'e' || word[0] == 'E' )
- {
- strcpy(hashtable[4]-> word, word);
- index++;
- }
- else if (word[0] == 'f' || word[0] == 'E' )
- {
- strcpy(hashtable[5]-> word, word);
- index++;
- }
- else if (word[0] == 'g' || word[0] == 'G' )
- {
- strcpy(hashtable[6]-> word, word);
- index++;
- }
- else if (word[0] == 'h' || word[0] == 'H' )
- {
- strcpy(hashtable[7]-> word, word);
- index++;
- }
- else if (word[0] == 'i' || word[0] == 'I' )
- {
- strcpy(hashtable[8]-> word, word);
- index++;
- }
- else if (word[0] == 'j' || word[0] == 'J' )
- {
- strcpy(hashtable[9]-> word, word);
- index++;
- }
- else if (word[0] == 'k' || word[0] == 'K' )
- {
- strcpy(hashtable[10]-> word, word);
- index++;
- }
- else if (word[0] == 'l' || word[0] == 'L' )
- {
- strcpy(hashtable[11]-> word, word);
- index++;
- }
- else if (word[0] == 'm' || word[0] == 'M' )
- {
- strcpy(hashtable[12]-> word, word);
- index++;
- }
- else if (word[0] == 'n' || word[0] == 'N' )
- {
- strcpy(hashtable[13]-> word, word);
- index++;
- }
- else if (word[0] == 'o' || word[0] == 'O' )
- {
- strcpy(hashtable[14]-> word, word);
- index++;
- }
- else if (word[0] == 'p' || word[0] == 'P')
- {
- strcpy(hashtable[15]-> word, word);
- index++;
- }
- else if (word[0] == 'q' || word[0] == 'Q' )
- {
- strcpy(hashtable[16]-> word, word);
- index++;
- }
- else if (word[0] == 'r' || word[0] == 'R' )
- {
- strcpy(hashtable[17]-> word, word);
- index++;
- }
- else if (word[0] == 's' || word[0] == 'S' )
- {
- strcpy(hashtable[18]-> word, word);
- index++;
- }
- else if (word[0] == 't' || word[0] == 'T' )
- {
- strcpy(hashtable[19]-> word, word);
- index++;
- }
- else if (word[0] == 'u' || word[0] == 'U' )
- {
- strcpy(hashtable[20]-> word, word);
- index++;
- }
- else if (word[0] == 'v' || word[0] == 'V' )
- {
- strcpy(hashtable[21]-> word, word);
- index++;
- }
- else if (word[0] == 'w' || word[0] == 'W' )
- {
- strcpy(hashtable[22]-> word, word);
- index++;
- }
- else if (word[0] == 'x' || word[0] == 'X' )
- {
- strcpy(hashtable[23]-> word, word);
- index++;
- }
- else if (word[0] == 'y' || word[0] == 'Y' )
- {
- strcpy(hashtable[24]-> word, word);
- index++;
- }
- else if (word[0] == 'z' || word[0] == 'Z' )
- {
- strcpy(hashtable[25]-> word, word);
- index++;
- }
- else
- {
- return false;
- }
- // strcpy(new_node-> word, word)
- }
- fclose(file);
- return true;
- }
- /**
- * Returns number of words in dictionary if loaded else 0 if not yet loaded.
- */
- unsigned int size(void)
- {
- // TODO
- return index;
- }
- /**
- * Unloads dictionary from memory. Returns true if successful else false.
- */
- bool unload(void)
- {
- // TODO
- for (int i = 0; i < 26; i++)
- {
- node* cursor = hashtable[i];
- while (cursor != NULL)
- {
- node *temp = cursor;
- cursor = cursor->next;
- free(temp);
- }
- }
- return true;
- }
Add Comment
Please, Sign In to add comment