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.
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.
C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter5: Repetition Statements
Section5.5: A Closer Look: Loop Programming Techniques
Problem 12E: (Program) Write a program that tests the effectiveness of the rand() library function. Start by...
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
I have written the following code but it is not working,
#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 valid1 = false, valid2 = false;
int i;
if (msg[0] == 'P')
{
valid1 = true;
}
for (i = 1; i <= strlen(msg) - 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;
}
}
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])
{
bool v1 = false, v2 = false, v3 = true;
int i;
int c = 0;
int 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;
}
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 2 steps with 2 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
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr