upsidedown

expt2-toc-dfa

Jan 31st, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. //#include "stdafx.h"
  2. #include<iostream>
  3. #include<string.h>
  4. using namespace std;
  5. int main()
  6. {
  7.     char a[13];
  8.     int x,state=0,i;
  9.     for(i=0;i<13;i++)
  10.     {
  11.         a[i]=0;
  12.     }
  13.     cout<<"enter the string\n";
  14.     cin>>a;
  15.     i=0;
  16.     while(a[i]!=0)
  17.     {
  18.         switch(state)
  19.         {
  20.         case 0:
  21.             if(a[i]=='a')
  22.             {  
  23.                 state=1;
  24.             }
  25.             else if((a[i]=='b')||(a[i]=='c'))
  26.             {
  27.                 state=0;
  28.             }
  29.             //flag=0;
  30.             break;
  31.         case 1:
  32.             if(a[i]=='b')
  33.             {   state=2;
  34.             }
  35.             else if(a[i]=='c')
  36.             {   state=0;}
  37.             else if(a[i]=='a')
  38.             {state=1;}
  39.             //flag=0;
  40.             break;
  41.         case 2:
  42.             if(a[i]=='c')
  43.             {
  44.              state=3;
  45.              //flag=1;
  46.             }
  47.             else if(a[i]=='a')
  48.             {state=1;
  49.             //flag=0;
  50.             }
  51.             else if(a[i]=='b')
  52.             {state=0;
  53.             //flag=0;
  54.             }
  55.            
  56.             break;
  57.         case 3:
  58.             if(a[i]=='a')
  59.             {   state=4;
  60.             }
  61.             else if((a[i]=='b')||(a[i]=='c'))
  62.             {   state=4;
  63.             }
  64.             //flag=1;
  65.             break;
  66.         case 4:
  67.             if(a[i]=='b')
  68.                 state=5;
  69.             else if(a[i]=='c')
  70.                 state=3;
  71.             else if(a[i]=='a')
  72.                 state=4;
  73.             //flag=1;
  74.             break;
  75.         case 5:
  76.             if(a[i]=='c')
  77.             {state=0;}
  78.             else if(a[i]=='b')
  79.             {   state=3;}
  80.             else if(a[i]=='a')
  81.             {state=4;}
  82.             //flag=1;
  83.             break;
  84.         default:
  85.             cout<<"error..........................";
  86.             break;
  87.         }
  88.             i++;
  89.    
  90.     }
  91.     cout<<"\n\n";
  92.         if((state==3)||(state==4)||(state==5))
  93.         {
  94.         cout<<"valid string";
  95.         }
  96.         else
  97.         {
  98.         cout<<"invalid string";
  99.         }
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment