Program to construct DFA accepting odd number of 0 s and odd number of 1 s | C ++ program for the above approach ; Function to check whether the given string is accepted by DFA or not ; Stores initial state of DFA ; Stores final state of DFA ; Stores previous state of DFA ; Iterate through the string ; Checking for all combinations ; Update the previous_state ; If final state is reached ; Otherwise ; Driver Code ; Given string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkValidDFA ( string s ) { int initial_state = 0 ; int final_state ; int previous_state = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ( s [ i ] == '0' && previous_state == 0 ) || ( s [ i ] == '1' && previous_state == 3 ) ) { final_state = 1 ; } else if ( ( s [ i ] == '0' && previous_state == 3 ) || ( s [ i ] == '1' && previous_state == 0 ) ) { final_state = 2 ; } else if ( ( s [ i ] == '0' && previous_state == 1 ) || ( s [ i ] == '1' && previous_state == 2 ) ) { final_state = 0 ; } else if ( ( s [ i ] == '0' && previous_state == 2 ) || ( s [ i ] == '1' && previous_state == 1 ) ) { final_state = 3 ; } previous_state = final_state ; } if ( final_state == 3 ) { cout << " Accepted " << endl ; } else { cout << " Not ▁ Accepted " << endl ; } } int main ( ) { string s = "010011" ; checkValidDFA ( s ) ; return 0 ; }