Write a C program that prompts the user for a series of letters. The program wil
ID: 3769042 • 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.
In general terms, determining if a given word can be made from the given letters will require counting how many distinct kinds of letters there are in the given letters and in the words. For example, assume that th word "bull" is in the dictionary. The word "bull" contains 1 'b' character, 2 'll' characters, and 1 'u' character. Now say the input letters were "alblldi". In "alblldi", we have enough 'b' characters for "bull", since "alblldi" contains at least 1 'b' character. Similarly, "alblldi" has enough 'l' characters for "bull", since "alblldi" contains at least 2 'l' characters. However, "alblldi" does not have at least 1 'u' character, and such we know that we cannot make "bull" from "alblldi".
Read in the dictionary words from a file. The name of the dictionary file is "dictionary.txt". Make the following assumptions about the dictionary 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 you cannot open the dictionary file, you should print "Failed to open dictionary file 'dictionary.txt'", followed by a new line, and then exit the program.
An example dictionary file:
ape apple
ball bill bull
foot
parrot pebble
season
zebras zoo
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 words in the dictionary that could be made in alphabetic order, as specified in the dictionary.
c. The program prints out what letters were type 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 an uppercase letter your program must convert it to lower case.
f. Your program must check if each character is a valid letter of the English alphabet, if it is not then that character is discarded.
For instance, the following would be some output from your program:
enter letters: alblldi
alblldi:
ball
bill
enter letters: moo
moo:
enter letters: bleppa
bleppa:
ape
apple
enter letters: exit
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.