personal portal to x M Inbox (1,084)-aidelals@buffai x C Home Chegg.com urses/EA
ID: 3736353 • Letter: P
Question
personal portal to x M Inbox (1,084)-aidelals@buffai x C Home Chegg.com urses/EAS240-$18/assessments/hw4/attachments/48 Q3) Use a state machine to copy a file but replace the word "steak" with the word "chicken". The input file is named menu.txt, and the output file is to be named output.txt. In other words, your code will open and read (character by character) from the input file menu.txt, and write (character by character) into a new file to be named output.txt. However, whenever you read the word "steak" you should write instead "chicken" into the output.txt, otherwise you write the exact same character that is read from the input menu.txt. State Machine: A state machine switches tasks based on events. They are typically used for simple controllers or sequence detectors. Think of an autonomous car. If it to either side, the vehicle will leave the road, and cause harm to it's passengers. Suppose the veers too far car had two states, 'adjust left' and 'adjust right'. The car may switch states to adjust the turning angle, thus keeping it's passengers safe and on the road. In this assignment we would like to detect the word "steak" in the menu file. The state is switched based on if the current character is correct and is in the right order to form the word "steak". If it is correct, move to the next character in the word. If it is wrong, write all the characters to the output file and go back to the initial state. This means that you read each character as it comes in and check if they are in the right order to form the word "steak". If the complete word "steak" is found, then write the word "chicken" in the output and go back to the initial state. You must use a switch statement. Consider the following pseudocode: Let state 1 be the initial state where looking for 's', state 2 be where 's' is detected and looking for 't' state 3 be where 'st' is detected and looking for 'e'. state 4 be where 'ste' is detected and looking for 'a', state 5 be where 'stea' is detected and looking for k In pseudocode: Start in State 1. State 1) get character if character 's' then goto state 2 else put character in output ßle and stay in state 1Explanation / Answer
/*Note in general space is not recognized in switch cases if we do switch on char hence some more logic was written to correct this , please follow it*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
FILE *inputFile,*outputFile;
inputFile = fopen(argv[1],"r");
if(inputFile == NULL){
printf("couldn't open given file hence returning ");
return 1;
}
outputFile = fopen("output.txt","w");
if(outputFile == NULL){
printf("couldn't open output file hence returning ");
return 1;
}
char ch;
int state = 0;
int space = 0;
while( (ch = fgetc(inputFile) )!= EOF){
switch (ch){
case 's':
if(state == 0){
++state;
ch = fgetc(inputFile);
if(ch == ' '){
space = 1;
break;
}
}
case 't':
if(state == 1){
++state;
ch = fgetc(inputFile);
if(ch == ' '){
space = 1;
break;
}
}
case 'e':
if(state == 2){
++state;
ch = fgetc(inputFile);
if(ch == ' '){
space = 1;
break;
}
}
case 'a':
if(state == 3){
++state;
ch = fgetc(inputFile);
if(ch == ' '){
space = 1;
break;
}
}
case 'k':
if(state == 4){
++state;
}
}
if(state == 5){
fputs("chicken",outputFile);
}else if(state == 0){
fputc(ch,outputFile);
}else{
fprintf(outputFile,"%.*s", state, "steak");
if(space){
fputc(' ',outputFile);
space = 0;
}
}
state = 0;
}
return (EXIT_SUCCESS);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.