include punctuation as part or à word ou libfs.c and libfs.h in your tar. Q3) Us
ID: 3732807 • Letter: I
Question
include punctuation as part or à word ou libfs.c and libfs.h in your tar. Q3) Use a state machine to copy a ile 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 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 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 svitch statement. Consider the following pseudocode: Let state 1 be the initial state where looking for 's', Problem_Set 3pdf (1).2 Practice Set 1.zip Practice Midterm.zip anyconnect-macos-...dmgExplanation / Answer
//steak_sm.c
#include<stdio.h>
#include<stdlib.h>
void main()
{FILE *fs,*fo;
int s=1,flag=1;char ch;
fs=fopen("menu.txt","r");
if(fs==NULL)
{printf("not opened");
exit(0);
}
fo=fopen("output.txt","w");
if(fo==NULL)
{printf("not opened");
exit(0);
}
while(flag==1)
{
switch( s )
{
case 1:
ch=fgetc(fs);
if(ch=='s')
{ s=2;break;}
else if(ch==EOF)
{ flag=0;break;}
else
{ fputc(ch,fo);
break;}
case 2:
ch=fgetc(fs);
if(ch=='t')
{ s=3; break;}
else if(ch==EOF)
{ flag=0; break;}
else
{fputc('s',fo);
fputc(ch,fo);
s=1;break;
}
case 3:
ch=fgetc(fs);
if(ch=='e')
{ s=4;break;}
else if(ch==EOF)
{flag=0;break;}
else
{ fputc('s',fo);fputc('t',fo);fputc(ch,fo);s=1;
break;
}
case 4:
ch=fgetc(fs);
if(ch=='a')
{s=5;break;}
else if(ch==EOF)
{flag=0;break;}
else
{ fputc('s',fo);fputc('t',fo);
fputc('e',fo);
fputc(ch,fo);s=1;
break;
}
case 5:
ch=fgetc(fs);
if(ch=='k')
{ s=1; fputc('c',fo);fputc('h',fo);
fputc('i',fo); fputc('c',fo);fputc('k',fo);
fputc('e',fo); fputc('n',fo);
break;}
else if(ch==EOF)
{flag=0;break;}
else
{ fputc('s',fo);fputc('t',fo);
fputc('e',fo);fputc('a',fo);
fputc(ch,fo);s=1;
break;
}
}
}
fclose(fs);fclose(fo);
printf("Done ");
}
//menu.txt
steak chicken are both same .. steak
steah steak steak
//output.txt
chicken chicken are both same .. chicken
steah chicken chicken
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.