I need help writing this C code. Here is the description:
Let's say we have a program called smart typing which does two things:
we would like to know, for each word in the dictionary, which letters have to be input by a keystroke and to know the average of keystrokes needed to type a word in a dictionary.
The program gets from the command line the name of an input file containing the dictionary an the name of an output file which will contain the solution.
The first line of the input file contains an integer N that is the number of words of the dictionary (1<=N<=20). Each of the next N lines contains a word of the dictionary. The length of each word is at most 50 lowercase letters from the English alphabet ={a,b,...,z}. Assume all words are different.
The program creates a file which will contains N+2 lines with the solution. The first line contains an integer N that is the number of words. Each of the next N lines contains a word of the dictionary indicating with an asterisk the position of the letter that is displayed automatically by the smart typing. The last line shows the average number of keystrokes needed to type a word in the given dictionary. The average must be displayed with 2 digits after the decimal point.
Example of input file:
4 hello hell heaven goodbye
Example of Output:
4
h*l*o h*l* h*a*** g****** 2.00
Further Explanation:
To type the word hello, we need to type always the first letter for any word, in this case is h, every word which starts with h has as next letter e, then "e" is displayed automatically and we get the sequence "he". The 3rd letter is "l" and not every word which starts with "he" has "l" as 3rd letter (e.g. heaven) so we need to press "l". After press "l" we have the sequence "hel". Every word that starts with "hel" has "l" as 4th letter (hello, hell), then "l" in the 4th position is displayed automatically and we get "hell". Finally, smart typing doesn't know if we want to type the word "hell" or "hello" that's why "o" is not displayed automatically and we need to press "o" to complete the word "hello". In this way we can determine which letters of "hello" are displayed automatically. The same logic is applied for each word.
The average number of keystrokes needed to type a word is the sum of keystrokes needed to type each word (no asterisks in the output for each word) over the total of words in the dictionary. For this example is:
(3+2+2+1)/4 = 2.00
Testing the program:
Here is an example of what is expected in the functionality of the program
Example Program_x Input1.txt Error! You must specify a filename for input and output Program_x Input.txt Output1.txt Error! Input.txt cannot be opened Program_x Input1.txt Output1.txt Output1.txt was created successfully!
In this ques ...Trie data structure can be used to solve the problem ..Here two functions are there
insert_in_trie(trie *root ,char word[ ]) ==>this function inserts the word in trie;
write_word(FILE *fptr,trie *root,char word[ ])==>this function writes the words as required in ques.
.....basically the letter will be typed when more words can be made by the present prefix or prefix is equal to some word in dictionary ==>this can be easily handled by trie
So at particular node in trie...the possible options are seen ..if unique option is there then letter would be typed automatically else the letter must be typed .
Code snippet and screenshots has been attached
#include <stdio.h>
#include <stdlib.h>
struct Trie{
struct Trie *child[26];//member containing info which different letter nodes it connects from this node
int isleaf;//tells whether the node here signifies the end of the word or not
};
typedef struct Trie trie; //giving alias name as trie
void initialize(trie *node)
{
node->isleaf=0; //initially leaf will be marked 0 since it is not leaf
for(int i=0;i<26;i++)
{
node->child[i]=NULL; //node is not having any child ...so make it null
}
}
int count_typed=0;//counts the total letters to be typed
void insert_in_trie(trie *root,char x[]) //function to insert the given word in trie
{
for(int i=0;x[i]!='\0';i++) //iterate until null character arrives
{
int req=x[i]-'a'; //get index representing character
if(root->child[req]==NULL) //this checks whether this particular letter is there or not (if not then create the node)
{
root->child[req]=(trie *)malloc(sizeof(trie));
initialize(root->child[req]);//initialize the node with initial state
}
root=root->child[req];
//move the required node
}
root->isleaf=1; // after traversing the whole word we will get the last node which will represent end of the word //and we will mark iseleaf as true
}
void write_word(FILE *fptr,trie *root,char word[])
{
int prev_finished=0;//holds the info whether any word was finished at pervious node or not
int curr_finshed=0;//holds the info whether any word was finished at current node or not
for(int i=0;word[i]!='\0';i++)
{
int options=0; //holds info of possible options from this node
for(int j=0;j<26;j++)
{
if(root->child[j]!=NULL) //checks whether the child[j] is present or not
{
if(root->child[j]->isleaf==1) //checks whether the child[j] represents the end of word
curr_finshed=1; //if condition above is true mark the current_finished as 1
options++; //increment the option
}
}
if(options>1||prev_finished==1) //if options is greater than 1 or any word was finished at previous node then character must be typed
{
fprintf(fptr,"%c",word[i]);
int index=word[i]-'a';//get index representing the child
root=root->child[index]; //move to the child
count_typed++; //increment the variable
}
else //this means only one option is there to traverse
{
fprintf(fptr,"%c",'*'); //so print *
//get the only child having not null value
for(int j=0;j<26;j++)
{
if(root->child[j]!=NULL)
{
root=root->child[j];
break;
}
}
}
prev_finished=curr_finshed; //make curr_finished as prev_finished
}
fprintf(fptr,"%s","\n");
}
int main()
{
FILE *fptr; //initialise the file pointer
int f=0;// checks whether file is opened successfully or not
int size=0;//represents the number of words
while(f==0)
{
char filename_input[100];//input filename
char filename_output[100];//output filename
scanf("%s",filename_input);//taking input
scanf("%s",filename_output);//taking input
if ((fptr = fopen(filename_input, "r")) == NULL) {
printf("Error! opening file");
continue;
// Program exits if file pointer returns NULL.
}
trie *root=(trie *)malloc(sizeof(trie));//creating trie node
initialize(root);//initialize root
fscanf(fptr,"%d",&size);//read the file to get size
char words[20][50];//to store words
for(int i=0;i<size;i++)
{
fscanf(fptr,"%s",words[i]);//read word from file
insert_in_trie(root,words[i]);//insert the word in trie
}
fclose(fptr);//close the file pointer
if ((fptr = fopen(filename_output, "w")) == NULL) {
printf("Error! opening file");
continue;
// Program exits if file pointer returns NULL.
}
f=1;//both files have been opened successfully
fprintf(fptr,"%d\n",size);//write size to file
for(int i=0;i<size;i++)
{
write_word(fptr,root,words[i]);//write the word to file
}
float Size=size;
fprintf(fptr,"%.2f",count_typed/Size);//write the required value
fclose(fptr);
}
}
Screenshots
I need help writing this C code. Here is the description: Let's say we have a...
Create the game hangman using c code: Program description In the game of hangman, one player picks a word, and the other player has to try to guess what the word is by selecting one letter at a time. If they select a correct letter, all occurrences of the letter are shown. If no letter shows up, they use up one of their turns. The player is allowed to use no more than 10 incorrect turns to guess what the...
please
write a C++ code
Problem A: Word Shadow Source file: shadow.cpp f or java, or.cj Input file: shadow.in in reality, when we read an English word we normally do not read every single letter of that word but rather the word's "shadow" recalls its pronunciation and meaning from our brain. The word's shadow consists of the same number of letters that compose the actual word with first and last letters (of the word) in their original positions while the...
Python Help Please! This is a problem that I have been stuck
on.I am only suppose to use the basic python coding principles,
including for loops, if statements, elif statements, lists,
counters, functions, nested statements, .read, .write, while, local
variables or global variables, etc. Thank you! I am using python
3.4.1. ***( The bottom photo is a continuation of the first
one)****
Problem statement For this program, you are to design and implement text search engine, similar to the one...
Please Use C++ Language. Thank you. Please I need the actual code. Donot post psudocode!! And also I have codes but just donot work so make sure that it works. Requested files: CrosswordGenerator.cpp, CrosswordGenerator.h, CrosswordGenerator_test.cpp CrosswordGenerator - Write a program that helps to generate a crossword puzzle by organizing words that share letters. For this assignment, you will write a program that forms the basis of a crossword puzzle generator. In order to create a crossword puzzle you need to...
Assignment 1 In this assignment you will be writing a tool to help you play the word puzzle game AlphaBear. In the game, certain letters must be used at each round or else they will turn into rocks! Therefore, we want to create a tool that you can provide with a list of letters you MUST use and a list of available letters and the program returns a list of all the words that contain required letters and only contain...
Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...
Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...
I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly program that does the following; 1. count and display the number of words in the user input string. 2. Flip the case of each character from upper to lower or lower to upper. For example if the user types in: "Hello thEre. How aRe yOu?" Your output should be: The number of words in the input string is: 5 The output string is : hELLO...
I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following:...
Question 2: Finding the best Scrabble word with Recursion using java Scrabble is a game in which players construct words from random letters, building on words already played. Each letter has an associated point value and the aim is to collect more points than your opponent. Please see https: //en.wikipedia.org/wiki/Scrabble for an overview if you are unfamiliar with the game. You will write a program that allows a user to enter 7 letters (representing the letter tiles they hold), plus...