NFA to accept strings that has atleast one character occurring in a multiple of 3 | C ++ implementation of the above approach ; NFA variable that keeps track of the state while transaction . ; This checks for invalid input . ; Function for the state Q2 ; State transitions ' a ' takes to Q4 , and ' b ' and ' c ' remain at Q2 ; Function for the state Q3 ; State transitions ' a ' takes to Q3 , and ' b ' and ' c ' remain at Q4 ; Function for the state Q4 ; State transitions ' a ' takes to Q2 , and ' b ' and ' c ' remain at Q3 ; Function for the state Q5 ; State transitions ' b ' takes to Q6 , and ' a ' and ' c ' remain at Q5 ; Function for the state Q6 ; State transitions ' b ' takes to Q7 , and ' a ' and ' c ' remain at Q7 ; Function for the state Q7 ; State transitions ' b ' takes to Q5 , and ' a ' and ' c ' remain at Q7 ; Function for the state Q8 ; State transitions ' c ' takes to Q9 , and ' a ' and ' b ' remain at Q8 ; Function for the state Q9 ; State transitions ' c ' takes to Q10 , and ' a ' and ' b ' remain at Q9 ; Function for the state Q10 ; State transitions ' c ' takes to Q8 , and ' a ' and ' b ' remain at Q10 ; Function to check for 3 a 's ; Function to check for 3 b 's ; Function to check for 3 c 's ; Driver Code ; If any of the states is true , that is , if either the number of a ' s ▁ or ▁ number ▁ of ▁ b ' s or number of c 's  is a multiple of three, then the string is accepted
#include <bits/stdc++.h> NEW_LINE int nfa = 1 ; int flag = 0 ; using namespace std ; void state1 ( char c ) { if ( c == ' a ' ) nfa = 2 ; else if ( c == ' b ' c == ' c ' ) nfa = 1 ; else flag = 1 ; } void state2 ( char c ) { if ( c == ' a ' ) nfa = 3 ; else if ( c == ' b ' c == ' c ' ) nfa = 2 ; else flag = 1 ; } void state3 ( char c ) { if ( c == ' a ' ) nfa = 1 ; else if ( c == ' b ' c == ' c ' ) nfa = 3 ; else flag = 1 ; } void state4 ( char c ) { if ( c == ' b ' ) nfa = 5 ; else if ( c == ' a ' c == ' c ' ) nfa = 4 ; else flag = 1 ; } void state5 ( char c ) { if ( c == ' b ' ) nfa = 6 ; else if ( c == ' a ' c == ' c ' ) nfa = 5 ; else flag = 1 ; } void state6 ( char c ) { if ( c == ' b ' ) nfa = 4 ; else if ( c == ' a ' c == ' c ' ) nfa = 6 ; else flag = 1 ; } void state7 ( char c ) { if ( c == ' c ' ) nfa = 8 ; else if ( c == ' b ' c == ' a ' ) nfa = 7 ; else flag = 1 ; } void state8 ( char c ) { if ( c == ' c ' ) nfa = 9 ; else if ( c == ' b ' c == ' a ' ) nfa = 8 ; else flag = 1 ; } void state9 ( char c ) { if ( c == ' c ' ) nfa = 7 ; else if ( c == ' b ' c == ' a ' ) nfa = 9 ; else flag = 1 ; } bool checkA ( string s , int x ) { for ( int i = 0 ; i < x ; i ++ ) { if ( nfa == 1 ) state1 ( s [ i ] ) ; else if ( nfa == 2 ) state2 ( s [ i ] ) ; else if ( nfa == 3 ) state3 ( s [ i ] ) ; } if ( nfa == 1 ) { return true ; } else { nfa = 4 ; } } bool checkB ( string s , int x ) { for ( int i = 0 ; i < x ; i ++ ) { if ( nfa == 4 ) state4 ( s [ i ] ) ; else if ( nfa == 5 ) state5 ( s [ i ] ) ; else if ( nfa == 6 ) state6 ( s [ i ] ) ; } if ( nfa == 4 ) { return true ; } else { nfa = 7 ; } } bool checkC ( string s , int x ) { for ( int i = 0 ; i < x ; i ++ ) { if ( nfa == 7 ) state7 ( s [ i ] ) ; else if ( nfa == 8 ) state8 ( s [ i ] ) ; else if ( nfa == 9 ) state9 ( s [ i ] ) ; } if ( nfa == 7 ) { return true ; } } int main ( ) { string s = " bbbca " ; int x = 5 ; if ( checkA ( s , x ) || checkB ( s , x ) || checkC ( s , x ) ) { cout << " ACCEPTED " ; } else { if ( flag == 0 ) { cout << " NOT ▁ ACCEPTED " ; return 0 ; } else { cout << " INPUT ▁ OUT ▁ OF ▁ DICTIONARY . " ; return 0 ; } } }