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: using System ; class GFG { static void DFA ( string str , int N ) { if ( N <= 1 ) { Console . Write ( " 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 { Console . Write ( " No " ) ; return ; } if ( count == N ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } static public void Main ( ) { string str = " CAABBAAB " ; int N = str . Length ; DFA ( str , N ) ; } }