upsidedown

SEM6:TOC Expt2-DFA

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