Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- int i;
- FILE *dict;
- typedef struct node
- {
- bool is_word;
- struct node *children[27];
- }
- node;
- node *root;
- // Loads dictionary into memory, returning true if successful else false
- bool load(const char *dictionary)
- {
- // open the dictionary
- dict = fopen(dictionary, "r");
- if (!dict)
- {
- fprintf(stderr, "Could not open\n");
- return 2;
- }
- root = malloc(sizeof(node));
- root->is_word = false;
- node *current_node;
- current_node = root;
- char s[45];
- // go through every list in dictionary
- for(i=0; fgets(s, 45, dict) != NULL; i++)
- {
- current_node = root;
- // find every letter of each word
- for(int j=0, n = strlen(s); j < n - 1; j++)
- {
- // turn the character to ascii
- char character = s[j];
- int ascii = 0;
- if(character >= 'a' && character <= 'z')
- {
- ascii = character - 97;
- }
- else if(character == '\'')
- {
- ascii = character - 13;
- }
- // if the char doesnt exist then make a new node
- if (current_node->children[ascii] == NULL)
- {
- node *new_node = malloc(sizeof(node));
- current_node->children[ascii] = new_node;
- current_node = new_node;
- }
- // if char exists then point current_node to the new node
- else if (current_node->children[ascii] != NULL)
- {
- current_node = current_node->children[ascii];
- }
- }
- // at the end of every word say that current_node is a word (because you got to the end of a word)
- current_node->is_word = true;
- }
- return true;
- }
- // Returns number of words in dictionary if loaded else 0 if not yet loaded
- unsigned int size(void)
- {
- return i;
- }
- // Returns true if word is in dictionary else false
- bool check(const char *word)
- {
- node *temp;
- temp = root;
- // go over every char of the word
- for (int k = 0, length = strlen(word); k < length; k++)
- {
- char lower_case_char = tolower(word[k]);
- int ascii = 0;
- if(lower_case_char >= 'a' && lower_case_char <= 'z')
- {
- ascii = lower_case_char - 97;
- }
- else if(lower_case_char == '\'')
- {
- ascii = lower_case_char - 13;
- }
- // if the character doesn't exist then return false
- if (temp->children[ascii] == NULL)
- {
- printf("mispelled: %d, %c\n", ascii, word[k]);
- return false;
- }
- // if the character does exist have temp point to the new character
- else if (temp->children[ascii] != NULL)
- {
- printf("maybe a word: %d\n", ascii);
- temp = temp->children[ascii];
- }
- }
- return temp->is_word;
- }
- bool unload1(node *current_node)
- {
- for(int j = 0; j < 27; j++)
- {
- if (current_node -> children[j] != NULL)
- {
- current_node = current_node->children[j];
- unload1(current_node);
- }
- }
- free(current_node);
- current_node = NULL;
- return false;
- }
- // Unloads dictionary from memory, returning true if successful else false
- bool unload(void)
- {
- // need to unload each node
- node *current_node;
- current_node = root;
- unload1(current_node);
- // need to close the file
- fclose(dict);
- return true;
- }
- int main(int argc, char *argv[])
- {
- if (argc != 2)
- {
- printf("error");
- return 1;
- }
- char *dictionary = argv[1];
- bool loaded = load(dictionary);
- printf(loaded ? "true\n" : "false\n");
- int n = size();
- printf("n = %i\n", n);
- char *word = "Ca'ts";
- check(word);
- unload();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement