I can't figure out what is the problem. Please help me. Sorry for posting my check and load functions. But as I understand the problem can be in check or in load. I'm using trie:
bool check(const char* word) { node* next_node = root; int j = 0; while (word[j] != '\0') { int children_index = 0; if (word[j] == '\'') { children_index = 26; } else if (isalpha(word[j])) { if (word[j] >= 65 && word[j] <= 90) { children_index = word[j] - 65; } else if (word[j] >= 97 && word[j] <= 112) { children_index = word[j] - 97; } } else { return false; }if (next_node -> children[children_index] != NULL) { next_node = next_node -> children[children_index]; } else { return false; } j++; next_node -> is_word = true; return true; } return false;
}
/** * Loads dictionary into memory. Returns true if successful else false. / bool load(const char dictionary) { FILE* dictionary_file = fopen(dictionary, "r"); if (dictionary_file == NULL) { printf("Can't open the file\n"); return false; }
root = calloc(1, sizeof(node)); if (root == NULL) { return false; } char word[LENGTH + 2]; // until the end of file while (fgets(word, LENGTH + 2, dictionary_file) != NULL) { node* next_node = root; int i = 0; while (word[i] != '\n') { int children_index = 0; if (word[i] == '\'') { children_index = 26; } else if (isalpha(word[i])) { if (word[i] >= 65 && word[i] <= 90) { children_index = word[i] - 65; } else if (word[i] >= 97 && word[i] <= 112) { children_index = word[i] - 97; } } else { return false; } if (next_node -> children[children_index] == NULL) { next_node -> children[children_index] = calloc(1, sizeof(node)); next_node = next_node -> children[children_index]; } else { next_node = next_node -> children[children_index]; } // break; i++; } next_node -> is_word = true; dictionary_size++; } fclose(dictionary_file); return true;
}
Thanks in advance!