Advertisement
Rakibul_Ahasan

Untitled

Mar 22nd, 2021
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. #include<conio.h>
  4.  
  5. #include<string.h>
  6.  
  7. #include<stdlib.h>
  8.  
  9. int main() {
  10.   char s[20], c;
  11.   int state = 0, i = 0;
  12.  
  13.   //clrscr();
  14.   //Enter a String: aaaabbbbb
  15.  
  16.   printf("\n Enter a string:");
  17.   gets(s);
  18.  
  19.   while (s[i] != '\0') {
  20.     switch (state) {
  21.        
  22.     case 0:
  23.       c = s[i++];
  24.       if (c == 'a')
  25.         state = 1;
  26.       else if (c == 'b')
  27.         state = 2;
  28.       else
  29.         state = 6;
  30.       break;
  31.      
  32.     case 1:
  33.       c = s[i++];
  34.       if (c == 'a')
  35.         state = 3;
  36.       else if (c == 'b')
  37.         state = 4;
  38.       else
  39.         state = 6;
  40.       break;
  41.      
  42.     case 2:
  43.       c = s[i++];
  44.       if (c == 'a')
  45.         state = 6;
  46.       else if (c == 'b')
  47.         state = 2;
  48.       else
  49.         state = 6;
  50.       break;
  51.      
  52.     case 3:
  53.       c = s[i++];
  54.       if (c == 'a')
  55.         state = 3;
  56.       else if (c == 'b')
  57.         state = 2;
  58.       else
  59.         state = 6;
  60.       break;
  61.      
  62.     case 4:
  63.       c = s[i++];
  64.       if (c == 'a')
  65.         state = 6;
  66.       else if (c == 'b')
  67.         state = 5;
  68.       else
  69.         state = 6;
  70.       break;
  71.      
  72.     case 5:
  73.       c = s[i++];
  74.       if (c == 'a')
  75.         state = 6;
  76.       else if (c == 'b')
  77.         state = 2;
  78.       else
  79.         state = 6;
  80.       break;
  81.      
  82.     case 6:
  83.       printf("\n %s is not recognised.", s);
  84.       exit(0);
  85.     }
  86.   }
  87.  
  88.   if (state == 1)
  89.     printf("\n %s is accepted under rule 'a'", s);
  90.   else if ((state == 2) || (state == 4))
  91.     printf("\n %s is accepted under rule 'a*b+'", s);
  92.   else if (state == 5)
  93.     printf("\n %s is accepted under rule 'abb'", s);
  94.    
  95.   getch();
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement