Build a DFA to accept Binary strings that starts or ends with "01" | C ++ program to check if a string either starts or ends with 01 ; Function for transition state A ; State transition to B if the character is 0 ; State transition to D if the character is 1 ; Function for transition state B ; Check if the string has ended ; State transition to C if the character is 1 ; State transition to D if the character is 0 ; Function for transition state C ; Function for transition state D ; State transition to D if the character is 1 ; State transition to E if the character is 0 ; Function for transition state E ; State transition to E if the character is 0 ; State transition to F if the character is 1 ; Function for transition state F ; State transition to D if the character is 1 ; State transition to E if the character is 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void stateA ( string ) ; void stateB ( string ) ; void stateC ( string ) ; void stateD ( string ) ; void stateE ( string ) ; void stateF ( string ) ; void checkstateA ( string n ) { if ( n [ 0 ] == '0' ) stateB ( n . substr ( 1 ) ) ; else stateD ( n . substr ( 1 ) ) ; } void stateB ( string n ) { if ( n . length ( ) == 0 ) cout << " string ▁ not ▁ accepted " ; else { if ( n [ 0 ] == '1' ) stateC ( n . substr ( 1 ) ) ; else stateD ( n . substr ( 1 ) ) ; } } void stateC ( string n ) { cout << " String ▁ accepted " ; } void stateD ( string n ) { if ( n . length ( ) == 0 ) cout << " string ▁ not ▁ accepted " ; else { if ( n [ 0 ] == '1' ) stateD ( n . substr ( 1 ) ) ; else stateE ( n . substr ( 1 ) ) ; } } void stateE ( string n ) { if ( n . length ( ) == 0 ) cout << " string ▁ not ▁ accepted " ; else { if ( n [ 0 ] == '0' ) stateE ( n . substr ( 1 ) ) ; else stateF ( n . substr ( 1 ) ) ; } } void stateF ( string n ) { if ( n . length ( ) == 0 ) cout << " string ▁ accepred " ; else { if ( n [ 0 ] == '1' ) stateD ( n . substr ( 1 ) ) ; else stateE ( n . substr ( 1 ) ) ; } } int main ( ) { string n = "0100101" ; checkstateA ( n ) ; return 0 ; }