Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool load(const char *dictionary)
- {
- //no need to consider the case senstive
- //create memory space for the first node (lenght depend of the word)
- root = malloc(sizeof(node));
- // note lenght min for a word is 2 letters (+1) and the max is 45 ( the longest word)
- // open the dictionnary file to read his content
- FILE *fp = fopen(dictionary, "r");
- if (fp == NULL)
- {
- printf("Could not open %s.\n", dictionary);
- return false;
- }
- //get dictionaryfile
- for (int c = fgetc(fp); c!= EOF; c = fgetc(fp))
- {
- if (isalpha(c))
- {
- letter_position = (c - 65);
- }
- else if(c == '\'')
- {
- letter_position = 26;
- }
- //start with the root to check
- if(root->children[letter_position] == NULL)
- {
- //make sure that next word start with the root
- temp_pointer = root->children[letter_position];
- }
- else
- {
- //check the next node
- temp_pointer = malloc(sizeof(node));
- // check if next node exist if not NULL
- if(temp_pointer->children[letter_position]) == NULL)
- else
- {
- //move to the next node
- temp_pointer = temp_pointer->children[letter_position];
- }
- else
- {
- temp_pointer->is_word = true;
- return true;
- }
- }
- }
- fclose(fp);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment