Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <strings.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <cs50.h>
- #include "dictionary.h"
- #include <ctype.h>
- typedef struct node
- {
- char word[LENGTH + 1];
- struct node *next;
- }
- node;
- node *table[26*3];
- bool check(const char *word)
- {
- node *cursor = table[hash(word)];
- while (cursor != NULL)
- {
- if (strcasecmp(cursor->word, word) == false)
- {
- cursor = cursor->next;
- }
- else
- {
- return true;
- }
- }
- return false;
- }
- unsigned int hash(const char *word)// to hash a-z 0-26, maybe use ascii value and subtract?
- {
- int val = 0;
- for (int i = 0; word[i] != '\0'; i++)
- {
- char c = word[i];
- val += c;
- }
- }
- bool load(const char *dictionary)
- {
- char word[LENGTH + 1]; // an array of characters
- FILE *dictr = fopen(dictionary,"r"); // a pointer to a file in read mode
- if (dictr == NULL) // if there's nothing there
- {
- return false;
- }
- char buffer[LENGTH+1];
- while (fscanf(dictr,"%s", buffer) == 1)
- /*
- scan strings one at a time from the pointer to the dictionary
- file in the form of a string
- while fscanf hasn't reached the end of the file
- */
- {
- fscanf(dictr,"%s",buffer);
- node *myword = malloc(sizeof(node));
- if (myword == NULL)
- {
- return false;
- }
- strcpy(myword->word,buffer);
- }
- return true;
- }
- unsigned int size(void)
- {
- int count = 0;
- int i = 0;
- while (table[i]->next != NULL)
- /*
- if i remember correctly this doesn't work...
- how else am I to know how many words
- are in the linked list?
- */
- {
- count++;
- i++;
- }
- if (count == 0)
- {
- return 0;
- }
- else
- {
- return count;
- }
- }
- bool unload(void)
- {
- node *cursor = table[hash(0)];
- while (cursor->next != NULL)
- {
- node *tmp = cursor;
- cursor = cursor->next;
- free(tmp);
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment