-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
86 lines (73 loc) · 1.54 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Include C++ header files
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
struct TrieNode
{
struct TrieNode* character[26];
bool isWord;
};
TrieNode* createNode()
{
TrieNode* newNode = new TrieNode();
int i;
for(i = 0; i < 26; i++)
{
newNode -> character[i] = NULL;
}
newNode -> isWord = false;
return newNode;
}
void updateTrie(TrieNode* root, string word)
{
int i = 0;
while(i < word.length())
{
if(root -> character[word[i] - 'a'] == NULL)
root -> character[word[i] - 'a'] = createNode();
root = root -> character[word[i] - 'a'];
i++;
}
// Now set isWord as true
// to indicate end of a word
root -> isWord = true;
}
void completeWord(TrieNode* root, string word) {
int i = 0;
while(i < word.length()) {
if(root -> character[word[i] - 'a'] == NULL) {
cout << "NO SUCH WORD NOT FOUND IN DICTIONARY !\n";
return;
}
root = root -> character[word[i] - 'a'];
i++;
}
if(root -> isWord)
cout << word << endl;
else {
}
}
int main()
{
// Variable Declaration
ifstream myfile;
string word;
// Opening the word dictionary
myfile.open("res/words.txt", ifstream::in);
// Creating and initializing the Trie
TrieNode* root = createNode();
// Reading each word from the file and updating the Trie
while(getline(myfile, word))
{
updateTrie(root, word);
}
cin >> word;
// Convert each letter into its lowercase
transform(word.begin(), word.end(), word.begin(), ::tolower);
completeWord(root, word);
// Closing the word dictionary
myfile.close();
return 0;
}