Write a C program that prompts the user for a series of letters. The program wil
ID: 3771572 • 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.
For instance, the following would be some output from the program:
Enter letters: alblldi
alblldi:
ball
bill
enter letters:
bleppa
bleppa:
ape
apple
enter letters: exit // (PROGRAM TERMINATES)
Explanation / Answer
#include <stdio.h> int main(void) { char word[12] = ""; char exit[12] = "exit"; boolean a=true; while (a) { printf("enter a new word (or) enter exit”); Scanf(“%s”,word); If(strcmp(word,exit)) { a=false; } Char *matchWords=getMatchWords(words); for(i = 0; i < matchWords.length; i++) { printf("state %d : %s ", i, matchWords [i]); } } Char[] getMatchWords(char[] word) { Char[100] res; FILE *fptr; if ((fptr=fopen("program.txt","r"))==NULL){ printf("Error! opening file"); exit(1); /* Program exits if file pointer returns NULL. */ } char temp[100]; for (i = 0; i < 100; ++i) { // Read a word from the file fscanf(f, "%s", temp); // Allocate memory for the word, because temp is too temporary boolean a= findTarget(word,temp); if(a==true) { } res[i]= temp; } } public static boolean findTarget( String target, String source ) { int target_len = target.length(); int source_len = source.length(); boolean found = false; for(int i = 0; ( i < source_len && !found ); ++i) { int j = 0; while( !found ) { if( j >= target_len ) { break; } /** * Learning Concept: * * String target = "for"; * String source = "Searching for a string within a string the hard way."; * * 1 - target.charAt( j ) : * The character at position 0 > The first character in 'Target' > character 'f', index 0. * * 2 - source.charAt( i + j) : * * The source strings' array index is searched to determine if a match is found for the * target strings' character index position. The position for each character in the target string * is then compared to the position of the character in the source string. * * If the condition is true, the target loop continues for the length of the target string. * * If all of the source strings' character array element position matches the target strings' character array element position * Then the condition succeeds .. */ else if( target.charAt( j ) != source.charAt( i + j ) ) { break; } else { ++j; if( j == target_len ) { found = true; } } } } return found; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.