Write a C program, called msg.c, that reads messages from an input file (or stdin) and verifies whether the messages are valid or not. We developed a protocol for reading messages from a device. There are different length messages. Each message has rules, described below. Messages are separated by a newline.. Each message is followed by a description, some examples, and a Deterministic Finite State Automaton (DFA) which recognizes the message. Before input is read, we are in state 1. We transition states on each character read. If, at the end of input, we are in an accepting state (double circle), then the message is valid. foo: Starts with an E followed by a string of digits 0 1 or 2 followed by an F . E.g.: E201022011101F eep: Starts with a P, followed by arbitrary number of BC (including none). E.g.: P PBCBCBC op: Starts with a Q . Followed by a string of 6 and 7 , where the number of 7 s must be odd. E.g.: Q7 Q66666676666 Q76767 ork: Starts with an M, followed by a foo or an eep. E.g.: ME2010201F MPBC Following is the code for 1st and 3rd case. Please complete the code for 2nd and 4th. #include #include #include bool foo(char msg[120]); bool eep(char msg[120]); bool op(char msg[120]); bool ork(char msg[120]); void main() { char fname[30],lyne[120]; FILE *filehandle; char *item; char rule[100],msg[120]; printf("Enter the filename: "); scanf("%s",fname); filehandle = fopen(fname, "r"); if (filehandle == NULL) { printf("Error Could not open"); return; } while (fgets(lyne,120,filehandle)) { //printf("%s",lyne); item = strtok(lyne,":"); strcpy( rule,item); //printf("%s",rule); item = strtok(NULL,":"); strcpy( msg,item); if(strcmp(rule,"foo")==0) { printf("\nMessage: %s",msg); if(foo(msg)) { //strcat(msg," OK"); printf("OK"); } else { //strcat(msg," FAIL"); printf("FAIL"); } } /* if(strcmp(rule,"eep")==0) { if(eep(msg)) { printf("OK\n"); } else { printf("FAIL\n"); } }*/ if(strcmp(rule,"op")==0) { printf("\nMessage: %s",msg); if(op(msg)) { //strcat(msg," OK"); printf("OK"); } else { //strcat(msg," FAIL"); printf("FAIL"); } } /* if(strcmp(rule,"ork")==0) { if(ork(msg)) { printf("OK\n"); } else { printf("FAIL\n"); } }*/ // printf("msg: %s",msg); } } bool foo(char msg[120]) { int len; len=strlen(msg); int valid1=0,valid2=0; int i; // printf("%c last %c",msg[0],msg[(len-3)]); // printf("%d",len); // for(i=0;i
Write a C program, called msg.c, that reads messages from an input file (or stdin) and verifies whether the messages are valid or not. We developed a protocol for reading messages from a device. There are different length messages. Each message has rules, described below. Messages are separated by a newline.. Each message is followed by a description, some examples, and a Deterministic Finite State Automaton (DFA) which recognizes the message. Before input is read, we are in state 1. We transition states on each character read. If, at the end of input, we are in an accepting state (double circle), then the message is valid. foo: Starts with an E followed by a string of digits 0 1 or 2 followed by an F . E.g.: E201022011101F eep: Starts with a P, followed by arbitrary number of BC (including none). E.g.: P PBCBCBC op: Starts with a Q . Followed by a string of 6 and 7 , where the number of 7 s must be odd. E.g.: Q7 Q66666676666 Q76767 ork: Starts with an M, followed by a foo or an eep. E.g.: ME2010201F MPBC Following is the code for 1st and 3rd case. Please complete the code for 2nd and 4th. #include #include #include bool foo(char msg[120]); bool eep(char msg[120]); bool op(char msg[120]); bool ork(char msg[120]); void main() { char fname[30],lyne[120]; FILE *filehandle; char *item; char rule[100],msg[120]; printf("Enter the filename: "); scanf("%s",fname); filehandle = fopen(fname, "r"); if (filehandle == NULL) { printf("Error Could not open"); return; } while (fgets(lyne,120,filehandle)) { //printf("%s",lyne); item = strtok(lyne,":"); strcpy( rule,item); //printf("%s",rule); item = strtok(NULL,":"); strcpy( msg,item); if(strcmp(rule,"foo")==0) { printf("\nMessage: %s",msg); if(foo(msg)) { //strcat(msg," OK"); printf("OK"); } else { //strcat(msg," FAIL"); printf("FAIL"); } } /* if(strcmp(rule,"eep")==0) { if(eep(msg)) { printf("OK\n"); } else { printf("FAIL\n"); } }*/ if(strcmp(rule,"op")==0) { printf("\nMessage: %s",msg); if(op(msg)) { //strcat(msg," OK"); printf("OK"); } else { //strcat(msg," FAIL"); printf("FAIL"); } } /* if(strcmp(rule,"ork")==0) { if(ork(msg)) { printf("OK\n"); } else { printf("FAIL\n"); } }*/ // printf("msg: %s",msg); } } bool foo(char msg[120]) { int len; len=strlen(msg); int valid1=0,valid2=0; int i; // printf("%c last %c",msg[0],msg[(len-3)]); // printf("%d",len); // for(i=0;i
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
- Write a C program, called msg.c, that reads messages from an input file (or stdin) and verifies whether the messages are valid or not.
- We developed a protocol for reading messages from a device. There are different length messages. Each message has rules, described below. Messages are separated by a newline..
- Each message is followed by a description, some examples, and a Deterministic Finite State Automaton (DFA) which recognizes the message. Before input is read, we are in state 1. We transition states on each character read. If, at the end of input, we are in an accepting state (double circle), then the message is valid.
foo:
Starts with an E followed by a string of digits 0 1 or 2 followed by an F . E.g.:
E201022011101F
eep:
Starts with a P, followed by arbitrary number of BC (including none). E.g.:
P
PBCBCBC
op:
Starts with a Q . Followed by a string of 6 and 7 , where the number of 7 s must be odd. E.g.:
Q7
Q66666676666
Q76767
ork:
Starts with an M, followed by a foo or an eep. E.g.:
ME2010201F
MPBC
Following is the code for 1st and 3rd case. Please complete the code for 2nd and 4th.
#include <stdio.h>
#include<string.h>
#include<stdbool.h>
bool foo(char msg[120]);
bool eep(char msg[120]);
bool op(char msg[120]);
bool ork(char msg[120]);
void main()
{
char fname[30],lyne[120];
FILE *filehandle;
char *item;
char rule[100],msg[120];
printf("Enter the filename: ");
scanf("%s",fname);
filehandle = fopen(fname, "r");
if (filehandle == NULL)
{
printf("Error Could not open");
return;
}
while (fgets(lyne,120,filehandle)) {
//printf("%s",lyne);
item = strtok(lyne,":");
strcpy( rule,item);
//printf("%s",rule);
item = strtok(NULL,":");
strcpy( msg,item);
if(strcmp(rule,"foo")==0)
{
printf("\nMessage: %s",msg);
if(foo(msg))
{
//strcat(msg," OK");
printf("OK");
}
else
{
//strcat(msg," FAIL");
printf("FAIL");
}
}
/* if(strcmp(rule,"eep")==0)
{
if(eep(msg))
{
printf("OK\n");
}
else
{
printf("FAIL\n");
}
}*/
if(strcmp(rule,"op")==0)
{
printf("\nMessage: %s",msg);
if(op(msg))
{
//strcat(msg," OK");
printf("OK");
}
else
{
//strcat(msg," FAIL");
printf("FAIL");
}
}
/*
if(strcmp(rule,"ork")==0)
{
if(ork(msg))
{
printf("OK\n");
}
else
{
printf("FAIL\n");
}
}*/
// printf("msg: %s",msg);
}
}
bool foo(char msg[120])
{
int len;
len=strlen(msg);
int valid1=0,valid2=0;
int i;
// printf("%c last %c",msg[0],msg[(len-3)]);
// printf("%d",len);
// for(i=0;i<len;i++)
// {
// printf("%c",msg[i]);
//}
if(msg[0]=='E' && msg[len-3]=='F') // please check the correct character position and take
{
valid1=1;
}
for(i=1;i<=len-4;i++)
{
if(msg[i]=='0'||msg[i]=='1'||msg[i]=='2')
{
valid2=1;
}
else
{
valid2=0;
}
// printf("\n%d %c",i,msg[i]);
}
if(valid1==1 && valid2==1)
{
return true;
}
//printf("%d%d",valid1,valid2);
return false;
}
bool eep(char msg[120])
{
bool v1=false;
if(msg[0]=='P')
{
v1=true;
}
return false;
}
bool op(char msg[120])
{
int l;
bool v1=false,v2=false,v3=true;
int i;
int c=0;
l=strlen(msg);
// printf("%d",l);
if(msg[0]=='Q')
{
v1=true;
}
for(int i=1;i<l-2;i++)
{
if(msg[i]=='6'||msg[i]=='7')
{
v2=true;
}
else
{
v2=false;
}
if(msg[i]=='7')
{
c++;
}
}
if(c%2==1)
{
v3=true;
}
if(v1=true && v2==true && v3==true)
{
return true;
}
return false;
}
bool ork(char msg[120])
{
return false;
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education