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

word length does not include punctuation as part of a word. You only need to sub

ID: 3739114 • Letter: W

Question

word length does not include punctuation as part of a word. You only need to submit your libfs.c and libfs.h in your tar. 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 aut 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 suitch 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: onomous car. If it veers too far Let state 1 be the initial state where looking for 's

Explanation / Answer

#include<stdio.h>

#include<fcntl.h>

int main()

{

int f1 = open("input.txt",O_RDONLY|O_CREAT); //Open file

int f2 = open("output.txt",O_WRONLY|O_TRUNC);

char cur[1]; //one char length char array to read a char from file

int detected_pos = -1; //an integer to point how much of the sub string is found

while(read(f1,cur,1)!=0)

{

switch(cur[0]) //current read charecter

{

case 's': //if it is a s the detected_pos set to 0

detected_pos = 0;

break;

case 't':

if(detected_pos == 0) //detected pos set to 1 if a 't' has been read and 's' has been read previously(by using detected_pos)

{

detected_pos = 1;

}

else //if previously 's' is not read then write 't' to file and set detected_pos to -1 likewise everything

{

write(f2,cur,1);

detected_pos=-1;

}

break;

case 'e':

if(detected_pos == 1)

{

detected_pos = 2;

}

else

{

write(f2,cur,1);

detected_pos=-1;

}

break;

case 'a':

if(detected_pos == 2)

{

detected_pos = 3;

}

else

{

write(f2,cur,1);

detected_pos=-1;

}

break;

case 'k':

if(detected_pos == 3) //if detected_pos ==3 and 'k' had read currently then write chicken to output file

{

write(f2,"chicken",7);

}

else //else write that char to output file and set detected_pos to -1

{

write(f2,cur,1);

detected_pos=-1;

}

break;

default: //if other than 's','t','e','a','k' had been read then write it to output file

write(f2,cur,1);

break;

}

}

return 0;

}