Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q3) Use a state machine to copy a file but replace the word \"steak with the wor

ID: 3349680 • Letter: Q

Question

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 veers too far to either side, the vehicle will leave the road, and cause harm to it's passengers. Suppose the 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 eurrent 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 svitch 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 Start in State 1 State 1) get character; if character 's then goto state 2 else put character in output file and stay in state 1 State 2) get character if character t' then goto state 3 else put 's character in output file and goto state 1 State 3) get character: if character e then goto state 4 else put 'st + character in output file and goto state 1 State 4) get character; if character a' then goto state 5 else put 'ste character' in output file and goto state 1 State 5) get character; if character k then put chicken' in output file and goto state 1 else put 'stea + character in output file and goto state 1 The program should only print Done. n' after the program is finished scanning the input file and writing to the output file. Remember to close the files at the end of your program Note: Do not use goto statements, use a switch statement. A sample input file menu.txt is provided for you to test your code. The expected output for this file is also given to you (expected output.txt). You must name your program steak sm.c and the output file output.txt. You only need to submit your source C file, i.e., steak sm.c, in your tar

Explanation / Answer

#include <stdio.h>

//Function to replace 'steak'from input file with 'chicken' in output file.

void transformFile(FILE fptr_R, FILE fptr_W, int state);

int main(void) {

//File pointers for reading and writing in files.

FILE * fptr_R;

FILE * fptr_W;

//Opening input file

if((fptr_R = fopen("menu.txt","r")) == NULL){

printf("Error! opening input file");

return 1;

}

//Opening output file

if((fptr_W = fopen("output.txt","w")) == NULL){

printf("Error! opening output file");

return 1;

}

transformFile(fptr_R, fptr_W, 1);

fclose(fptr_R);

fclose(fptr_W);

//Opening result file to show the output on terminal

if((fptr_R =

fopen("output.txt","r")) ==

NULL){

printf("Error! opening input file");

return 1;

}

char c;

while ((c = (char)fgetc(fptr_R)) != EOF){

printf("%c",c);

}

fclose(fptr_R);

return 0;

}

void transformFile(FILE fptr_R, FILE fptr_W, int state)

{

//Read a char

char c = (char)fgetc(fptr_R);

//Check if the end of file is reached.

if(c == EOF)

return;

//State Machine: Taking action based on 'state' value

switch (state) {

case 1 : if (c=='s') {

//'s' found, looking for t now state = 2;

}

else {

//not 'steak', writing the stacked characters.

fputc(c, fptr_W);

}

break;

case 2 : if (c=='t') {

//'st' found, looking for e now

state = 3;

}

else {

//not 'steak', writing the stacked characters.

fputc('s', fptr_W);

fputc(c, fptr_W);

state = 1;

}

break;

case 3 : if (c=='e') {

//'ste' found, looking for a now

state = 4;

}

else {

//not 'steak', writing the stacked characters.

fputc('s', fptr_W);

fputc('t', fptr_W);

fputc(c, fptr_W);

state = 1;

}

break;

case 4 : if (c=='a')

{

//'stea' found, looking for k now state = 5;

}

else {

//not 'steak', writing the stacked characters.

fputc('s', fptr_W);

fputc('t', fptr_W);

fputc('e', fptr_W):

fputc(c, fptr_W);

state = 1;

}

break;

case 5 : if (c=='k') {

//'steak' found, replacing it with chicken in output file.

fputc('c', fptr_W);

fputc('h', fptr_W);

fputc('i', fptr_W);

fputc('c', fptr_W);

fputc('k', fptr_W);

fputc('e', fptr_W);

fputc('n', fptr_W);

state = 1;

}

else {

//not 'steak', writing the stacked characters.

fputc('s', fptr_W);

fputc('t', fptr_W);

fputc('e', fptr_W):

fputc('a', fptr_W);

fputc(c, fptr_W);

state = 1;

}

break;

}

return transformFile(fptr_R, fptr_W, state);

}