Advertisement
Nusrat_Ullah

strmtch

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