Write a program to identify whether a binary series is valid or not following a condition ( condition: even 0's and odd 1's )

 

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    
int table[4][2]={{3,1},{2,0},{1,3},{0,2}};
    int state=0, check=1, i;
    char str[100];
    cout<<"Enter a String : ";
    gets(str);
    for(i=0; i<strlen(str); i++)
    {
        if(str[i]=='0')
            state=table[state][0];
        else if(str[i]=='1')
            state=table[state][1];
        else
            check=0;
    }
    if(state==1 && check==1)
        cout<<"This is a valid string";
    else
        cout<<"This is not a valid string";
    return 0;
}


Sample input: 
Enter a String : 1000

Sample output:
This is not a valid string

Post a Comment

Previous Post Next Post