1 //===-- dictionary.c ---------------------------------------------*- C -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===---------------------------------------------------------------------===//
13 typedef struct tree_node
{
15 struct tree_node
*left
;
16 struct tree_node
*right
;
19 /* Given a char*, returns a substring that starts at the first
20 alphabet character and ends at the last alphabet character, i.e. it
21 strips off beginning or ending quotes, punctuation, etc. */
23 char *strip(char **word
) {
25 int len
= strlen(start
);
26 char *end
= start
+ len
- 1;
28 while ((start
< end
) && (!isalpha(start
[0])))
31 while ((end
> start
) && (!isalpha(end
[0])))
43 /* Given a binary search tree (sorted alphabetically by the word at
44 each node), and a new word, inserts the word at the appropriate
47 void insert(tree_node
*root
, char *word
) {
51 int compare_value
= strcmp(word
, root
->word
);
53 if (compare_value
== 0)
56 if (compare_value
< 0) {
57 if (root
->left
!= NULL
)
58 insert(root
->left
, word
);
60 tree_node
*new_node
= (tree_node
*)malloc(sizeof(tree_node
));
61 new_node
->word
= strdup(word
);
62 new_node
->left
= NULL
;
63 new_node
->right
= NULL
;
64 root
->left
= new_node
;
67 if (root
->right
!= NULL
)
68 insert(root
->right
, word
);
70 tree_node
*new_node
= (tree_node
*)malloc(sizeof(tree_node
));
71 new_node
->word
= strdup(word
);
72 new_node
->left
= NULL
;
73 new_node
->right
= NULL
;
74 root
->right
= new_node
;
79 /* Read in a text file and storea all the words from the file in a
80 binary search tree. */
82 void populate_dictionary(tree_node
**dictionary
, char *filename
) {
86 in_file
= fopen(filename
, "r");
88 while (fscanf(in_file
, "%s", word
) == 1) {
89 char *new_word
= (strdup(word
));
90 new_word
= strip(&new_word
);
91 if (*dictionary
== NULL
) {
92 tree_node
*new_node
= (tree_node
*)malloc(sizeof(tree_node
));
93 new_node
->word
= new_word
;
94 new_node
->left
= NULL
;
95 new_node
->right
= NULL
;
96 *dictionary
= new_node
;
98 insert(*dictionary
, new_word
);
103 /* Given a binary search tree and a word, search for the word
104 in the binary search tree. */
106 int find_word(tree_node
*dictionary
, char *word
) {
107 if (!word
|| !dictionary
)
110 int compare_value
= strcmp(word
, dictionary
->word
);
112 if (compare_value
== 0)
114 else if (compare_value
< 0)
115 return find_word(dictionary
->left
, word
);
117 return find_word(dictionary
->right
, word
);
120 /* Print out the words in the binary search tree, in sorted order. */
122 void print_tree(tree_node
*dictionary
) {
126 if (dictionary
->left
)
127 print_tree(dictionary
->left
);
129 printf("%s\n", dictionary
->word
);
131 if (dictionary
->right
)
132 print_tree(dictionary
->right
);
135 int main(int argc
, char **argv
) {
136 tree_node
*dictionary
= NULL
;
147 populate_dictionary(&dictionary
, filename
);
148 fprintf(stdout
, "Dictionary loaded.\nEnter search word: ");
149 while (!done
&& fgets(buffer
, sizeof(buffer
), stdin
)) {
151 int len
= strlen(word
);
154 for (i
= 0; i
< len
; ++i
)
155 word
[i
] = tolower(word
[i
]);
157 if ((len
> 0) && (word
[len
- 1] == '\n')) {
158 word
[len
- 1] = '\0';
162 if (find_word(dictionary
, word
))
163 fprintf(stdout
, "Yes!\n");
165 fprintf(stdout
, "No!\n");
167 fprintf(stdout
, "Enter search word: ");
170 fprintf(stdout
, "\n");