Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include "dictionary.h"
- /**
- * Returns true if word is in dictionary else false.
- */
- typedef struct node
- {
- bool is_word;
- // size of 26 alphabets + one apostrophe character = 27
- struct node* dependent[27];
- }
- node;
- // declare root node
- node* root = NULL;
- bool load(const char* dictionary)
- {
- // initialize root
- root = calloc(1, sizeof(node));
- // identify each character of the words in dictionary
- for (int i = 0, l = strlen(dictionary); i < l; i++)
- {
- // initializing 1st letter of the word
- int k = dictionary[0] - 'a';
- // placing alphabetical values starting from 0
- int j = dictionary[i] - 'a';
- // if 1st position is NULL
- if (root -> dependent[k] == NULL)
- {
- root -> dependent[k] = dictionary[0];
- // if single character word
- if (i == l - 1)
- {
- root -> is_word = true;
- }
- }
- // if root position is not NULL and is a single character word
- else if (root -> dependent[k] != NULL && i == l - 1)
- {
- root -> is_word = true;
- }
- else
- {
- // create and initialize new node
- node* new_node = calloc(1, sizeof(node));
- // store character in new node pointed to by the root node
- root -> dependent[k] -> (new_node[i] -> dependent[j]) = dictionary[i];
- if (i == l - 1)
- {
- root -> is_word = true;
- }
- }
- }
- return false;
- }
Add Comment
Please, Sign In to add comment