Program to build a DFA to accept strings that start and end with same character | C ++ Program for DFA that accepts string if it starts and ends with same character ; States of DFA ; Function for the state Q1 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q2 ; Function for the state Q2 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q2 ; Function for the state Q3 ; Condition to check end of string ; State transitions ' a ' takes to q4 , and ' b ' takes to q3 ; Function for the state Q4 ; Condition to check end of string ; State transitions ' a ' takes to q4 , and ' b ' takes to q3 ; Function for the state Q0 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q3 ; Driver Code ; Since q0 is the starting state Send the string to q0
#include <bits/stdc++.h> NEW_LINE using namespace std ; void q1 ( string , int ) ; void q2 ( string , int ) ; void q3 ( string , int ) ; void q4 ( string , int ) ; void q1 ( string s , int i ) { if ( i == s . length ( ) ) { cout << " Yes ▁ STRNEWLINE " ; return ; } if ( s [ i ] == ' a ' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } void q2 ( string s , int i ) { if ( i == s . length ( ) ) { cout << " No ▁ STRNEWLINE " ; return ; } if ( s [ i ] == ' a ' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } void q3 ( string s , int i ) { if ( i == s . length ( ) ) { cout << " Yes ▁ STRNEWLINE " ; return ; } if ( s [ i ] == ' a ' ) q4 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } void q4 ( string s , int i ) { if ( i == s . length ( ) ) { cout << " No ▁ STRNEWLINE " ; return ; } if ( s [ i ] == ' a ' ) q4 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } void q0 ( string s , int i ) { if ( i == s . length ( ) ) { cout << " No ▁ STRNEWLINE " ; return ; } if ( s [ i ] == ' a ' ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } int main ( ) { string s = " abbaabb " ; q0 ( s , 0 ) ; }