Monday, 30 September 2013

Check validity of a string

#include<iostream>
#include<string.h>
using namespace std;
void main()
{
       char st[20];
       int state=0,le,i=0,f=0,flg=0;
       cout<<"Enter a sting : ";
       gets(st);
       le=strlen(st);
       do
       {
              char c=st[i];
              switch(state)
              {
              case 0: if(c=='0')
                           {
                                  state=0;
                                  le--; flg=0;                     
                           }
                           else if(c=='1')
                           {
                                  state=1;
                                  le--; flg=0;
                           }                         
                           else
                           { f=1; break; }
                           break;
              case 1: if(c=='0'||c=='1')
                           {
                                  state=2;
                                  le--; flg=1;                     
                           }
                           else
                           { f=1; break; }
                           break;
              case 2: if(c=='0'||c=='1')
                           {
                                  state=2;
                                  le--; flg=2;                     
                           }
                           else
                           { f=1; break; }
                           break;
              } i++;       
       }
       while(le!=0 && f!=1);
       if(le==0 && flg==2)
       { cout<<"valid input"; }
       else
       { cout<<"invalid input"; }
}

Enter a sting : 1011010
valid input Press any key to continue . . .








No comments:

Post a Comment