text: Program to construct DFA for Regular Expression C ( A + B ) + | C ++ program for the above approach ; Function to find whether the given string is Accepted by the DFA ; If n <= 1 , then print No ; To count the matched characters ; Check if the first character is C ; Traverse the rest of string ; If character is A or B , increment count by 1 ; If the first character is not C , print - 1 ; If all characters matches ; Driver Code
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; void DFA ( string str , int N ) { if ( N <= 1 ) { cout << " No " ; return ; } int count = 0 ; if ( str [ 0 ] == ' C ' ) { count ++ ; for ( int i = 1 ; i < N ; i ++ ) { if ( str [ i ] == ' A ' str [ i ] == ' B ' ) count ++ ; else break ; } } else { cout << " No " ; return ; } if ( count == N ) cout << " Yes " ; else cout << " No " ; } int main ( ) { string str = " CAABBAAB " ; int N = str . size ( ) ; DFA ( str , N ) ; return 0 ; }