text: DFA for Strings not ending with " THE " | dfa tells the number associated with the present state ; This function is for the starting state ( zeroth ) of DFA ; On receiving ' T ' or ' t ' goto first state ( 1 ) ; This function is for the first state of DFA ; On receiving ' T ' or ' t ' goto first state ( 1 ) ; On receiving ' H ' or ' h ' goto second state ( 2 ) ; else goto starting state ( 0 ) ; This function is for the second state of DFA ; On receiving ' E ' or ' e ' goto third state ( 3 ) else goto starting state ( 0 ) ; This function is for the third state of DFA ; On receiving ' T ' or ' t ' goto first state ( 1 ) else goto starting state ( 0 ) ; store length of string ; Driver Code
code: < ? php $ dfa = 0 ; function start ( $ c ) { global $ dfa ; if ( $ c == ' t ' $ c == ' T ' ) $ dfa = 1 ; } function state1 ( $ c ) { global $ dfa ; if ( $ c == ' t ' $ c == ' T ' ) $ dfa = 1 ; else if ( $ c == ' h ' $ c == ' H ' ) $ dfa = 2 ; else $ dfa = 0 ; } function state2 ( $ c ) { global $ dfa ; if ( $ c == ' e ' $ c == ' E ' ) $ dfa = 3 ; else $ dfa = 0 ; } function state3 ( $ c ) { global $ dfa ; if ( $ c == ' t ' $ c == ' T ' ) $ dfa = 1 ; else $ dfa = 0 ; } function isAccepted ( $ str ) { global $ dfa ; $ len = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( $ dfa == 0 ) start ( $ str [ $ i ] ) ; else if ( $ dfa == 1 ) state1 ( $ str [ $ i ] ) ; else if ( $ dfa == 2 ) state2 ( $ str [ $ i ] ) ; else state3 ( $ str [ $ i ] ) ; } return ( $ dfa != 3 ) ; } $ str = " forTHEgeeks " ; if ( isAccepted ( $ str ) == true ) echo " ACCEPTED STRNEWLINE " ; else echo " NOT ▁ ACCEPTED STRNEWLINE " ; ? >