write a dictionary program that uses a tree structure. Next have the user provid
ID: 3769402 • Letter: W
Question
write a dictionary program that uses a tree structure.
Next have the user provide the name of a text (*.txt) file with a dictionary of English words in it.
Next it is to read in the dictionary of words from the input text file. Make certain to have this in a try block in case a bad file name was input. If the program cannot open the file for input, it must prompt the user again for a file name and continue to do this until a good name is provided. The file is to have one word on each line and may be of any length.
You must reject (ignore and display an error message) any words being input that have non-letters in them, that is,something like all13 or f@t. So no hyphenated words. But your program is to continue running after displaying the error messages as if nothing was wrong except that the rejected words will not be placed in the dictionary.
All acceptable words input should be converted so that all letters are in lower case.
The program is to then create a tree structure from the input words.
I would suggest using a linked list with up to 26 branches at each node for this, but you may use some other treestructure if you wish.
The program will then loop asking the user to input a word which the program will check to see if it is in the dictionary or not using the created tree. If the word is in the dictionary, the program will display the message: “xxxxx is in the dictionary” where xxxxx is the input word. If the word is not found in the dictionary, the program is to display the message: “xxxxx is not in the dictionary”. In either event, the program is to then ask for another word to be input and check if it is in the dictionary or not. This will continue until an empty string is input which will terminate the program.
Note, you must make certain that your program can handle words like the following in the dictionary: “a”, “an”, “ant”, “another” etc. and successfully find them if they are input. You may want to use a flag indicating that a node may be the end of a word, but that additional nodes may follow because another word (or words) has the same start but more letters.
Explanation / Answer
Both following files:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define tab_size 29 /* table size */
#define word_len 24 /* max length of word */
.h file:
/* structure to be used for each word */
typedef struct list {
char *word;
struct list *next;
} WORD;
/* create table */
WORD **create_tab(int size) {
int i = 0;
WORD **hash_tab;
hash_tab = malloc(sizeof(WORD*) * size); /*allocate memory */
for (; i<size; i++) /* initialize elements */
hash_tab[i]=NULL;
hash_tab[i]=NULL;
return hash_tab;
}
/* search for word in table; returns 1 if found, 0 otherwise */
int search(WORD **tb, char *w) {
WORD *htmp/*, *hprv*/;
unsigned long hash = (hash_f(w) % tab_size); /* hash_f is Dan Bernstein's hash function; modulo by tab_size to make sure it fits */
for (htmp = tb[hash]; (htmp != NULL) && (strcmp(htmp->word, w) != 0); / htmp = htmp->next) /* follow chained array of respective cell until word is found or until the end */
{
;
}
if (htmp == NULL) return 0;
return 1;
}
/* insert new WORD with word w at the beginning of the chained array of the respective cell */
void insert(WORD **ht, char *w) {
WORD *htmp;
unsigned long hash = (hash_f(w) % tab_size); /* hash_f is Dan Bernstein's hash function; modulo by tab_size to make sure it fits */
htmp = malloc( sizeof(*htmp) ); /* allocate memory for new WORD */
htmp->word = calloc(word_len+1,sizeof(char)); /* allocate memory for new word with max word length plus one character to make sure there's no buffer overflow in the next line*/
strncpy(htmp->word, w, word_len); /* copy w to word */
htmp->next = ht[hash]; /* new WORD now points to content of the respective table cell */
ht[hash] = htmp; /* table cell now points to new WORD */
}
/* receive empty table and create the whole dictionary in memory word by word */
WORD **make(WORD **dic;) {
char w[word_len];
FILE *dictionary;
dictionary = fopen("dictionary.txt","r");
while ((fgets( w, word_len, dictionary )) != NULL)
insert(dic, w);
fclose(dictionary);
return dic;
}
int main() {
WORD **dic;
char w[word_len];
dic = create_tab(tab_size); /* create the table */
dic = make(dic); /* insert all entrys of dictionary in table */
printf("Insert a word in lowercase: ");
if ((scanf("%s",w)) == 0) return 0; /* if I didn't somehow verify the scanf it would return an error */
else if (search(dic, w) == 1) printf("The word %s exists in the dictionary ",w);
else printf("The word %s does not exist in the dictionary",w);
return 0;
}
thank you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.