Write a C program that prompts the user for a series of letters. The program wil
ID: 3771569 • Letter: W
Question
Write a C program that prompts the user for a series of letters. The program will read in the letters and print out which dictionary words can be made from the provided letters. The dictionary is provided as a text file.
Assumptions about the dictionary.txt file:
a) Each line contains the words that begin with a particular letter
b) The words in each line are separated by tab
c) Each word consists only of lowercase letters in the range a-z
d) The maximum length of a single line is 99 characters
e) The maximum number of words is 100 f) If the file cannot be opened, print "Failed to open dictionary file ".
The program must meet the following requirements:
a) the program keeps asking the user for letters until the letters are exit.
b) the program prints the words in the dictionary that could be made in alphabetical order, as specified in the dictionary
c) the program prints out what letters were typed in, followed by a colon (:), followed by a list of the words that could be made (if any).
d) each word that could be made is prefixed by a tab character ( )
e) if the user types and uppercase letter your program us converted to a lowercase
f) you're program must check if each character is a valid letter of the English alphabet, if it is not in the character is disregarded.
THIS IS WHAT IS IN THE FILE (dictionary.txt) THE PROGRAM HAS TO READ FROM:
ape apple
ball bill bull
foot
parrot peeble
season
zebras zoo
Explanation / Answer
#include <stdio.h>
int main(){
FILE *fp;
fp = fopen("output.txt","r");
if (!fp){
printf("%s ", "Can not open file ");
return 0;
}
char line[100];
char store[100][100];
int k = 0;
while (fgets(line, sizeof(line), fp) != NULL){
sscanf(line, "%s %[^ ]", store[k++], store[i++]);
}
char ch[100];
while (1){
scanf("%s",&ch);
for (int i = 0; i < strlen(ch); i++)
ch[i] = tolower(ch[i]);
if (strcmp(ch,"exit") == 0)
break;
printf("%s : ", ch);
for (int i = 0; i < k; i++){
char res[100] = store[k];
int m = 0;
for (int x = 0; x < strlen(res); x++){
for (int y = 0; y < strlen(ch); y++){
if (ch[y] == res[x])
m = 1;
}
if (m == 0)
break;
}
if (m == 1)
printf("%s ", res);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.