Develop a program in C that:(File handling) 1) Reads words from a file called wo
ID: 3764292 • Letter: D
Question
Develop a program in C that:(File handling)
1) Reads words from a file called words, which contains one word per line.Each word has maximum
length to M.The number of words in the file is equal to N.
2) For each word that you read form the file, create a function (Insert) that creates a node in a Binary Search Tree with the name dictionary.Each node of the tree must contain one word.The left child must contain a word that it is lexicographically smaller while the right child contains a lexicographically bigger one.
3) When you finish reading the file, ask form the user to give a word as input.
4) Create a function SearchWord which searches the tree for the given, by the user, word. The function should return 1 if the word is found or 0 if the word does not exist in the tree.
5) Create a function PrintFile which prints the word exactly as they appear in the file.
6) Create a function PrintSorted that prints all the words in a lexicographical order.
## : Choose a value for M and N.N should be greater than 10.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 100 //Maximum letters in word
#define N 5 //Number of words in file
int compare( char* a, char* b ) //return true if a>b
{
int i = 0;
while(a[i]!=''||b[i]!='')
{
if(a[i]<b[i])
return 0;
else if(a[i]>b[i])
return 1;
else
i++;
}
if(a[i]==''&&b[i]=='')
return -1;
else if(a[i]=='')
return 0;
else
return 1;
}
struct btnode
{
char value[M];
struct btnode *l;
struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;
char global_value[M];
int flag = 1;
/* To create a node */
void create( char* data )
{
temp = (struct btnode *)malloc(1*sizeof(struct btnode));
strcpy(temp->value,data);
temp->l = temp->r = NULL;
}
/* Function to search the appropriate position to insert the new node */
void search(struct btnode *t)
{
if (compare(temp->value,t->value) && (t->r != NULL)) /* value more than root node value insert at right */
search(t->r);
else if (compare(temp->value , t->value) && (t->r == NULL))
t->r = temp;
else if (!compare(temp->value , t->value) && (t->l != NULL)) /* value less than root node value insert at left */
search(t->l);
else if (!compare(temp->value , t->value) && (t->l == NULL))
t->l = temp;
}
int SearchWord(struct btnode *t, char* key)
{
if(compare(key,t->value) == -1)
return 1;
if (compare(key,t->value) && (t->r != NULL)) /* value more than root node value insert at right */
SearchWord(t->r, key);
else if (compare(key,t->value) && (t->r == NULL))
return 0;
else if (!compare(key,t->value) && (t->l != NULL)) /* value less than root node value insert at left */
SearchWord(t->l, key);
else if (!compare(key,t->value) && (t->l == NULL))
return 0;
}
/* recursive function to perform inorder traversal of tree */
void PrintSorted(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
if (t->l != NULL)
PrintSorted(t->l);
printf("%s ", t->value);
if (t->r != NULL)
PrintSorted(t->r);
}
/* To insert a node in the tree */
void insert( char* data )
{
create( data );
if (root == NULL)
root = temp;
else
search(root);
}
void PrintFile(char words_in_file[N][M])
{
int i;
for(i=0;i<N;i++)
printf("%s ", words_in_file[i]);
}
int main()
{
int ch;
FILE * fp;
char * line = NULL;
size_t len = 0;
char word[M];
char words_in_file[N][M];
int i,j;
fp = fopen("sample.txt", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
j = 0;
while (getline(&line, &len, fp) != -1) {
for(i=0;line[i]!=' '&&line[i]!='';i++);
line[i] = '';
strcpy(words_in_file[j++],line);
insert(line);
}
fclose(fp);
printf("Enter a word to search:");
scanf("%s",&word);
if(SearchWord(root,word))
printf("Word exists ");
else
printf("Word does not exists ");
printf(" Words as they appear in file ");
PrintFile(words_in_file);
printf(" Words in Sorted format ");
PrintSorted(root);
if (line)
free(line);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.