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...



  • 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 aDeterministic 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 anaccepting 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:


Startswith an M, followed by afoo or aneep. E.g.:



ME2010201F
MPBC




I have written the following code but it is not working,





#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<>

  // {

  // 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 <>

  {

    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 <>

  {

    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 <>

 {

   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 <>

  {

    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;

}

Jun 05, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here