DFA for Strings not ending with “THE” in C++?


To use Deterministic Finite Automaton(DFA) to find strings that aren’t ending with the substring “THE”. We should keep that in mind that any variation of the substring “THE” like “tHe”, “The” ,”ThE” etc should not be at the end of the string.

First, we define our dfa variable and initialise it to 0 which keeps our track of state. It is incremented on each character matched.

int dfa = 0;

The begin(char c) method takes a character and checks if its ‘t’ or ‘T’ and go to first state i.e 1.

void begin(char c){
   if (c == 't' || c == 'T')
   dfa = 1;
}

The firstState(char c) method checks the first character and assigns dfa based on that value. If c is ‘t’ or ‘T’ then we go to first state else if c is ‘h’ or ‘H’ we go to second state and finally if it’s some other character then we go to the starting state i.e 0.

void firstState(char c){
   if (c == 't' || c == 'T')
      dfa = 1;
   else if (c == 'h' || c == 'H')
      dfa = 2;
   else
      dfa = 0;
}

The secondState(char c) takes a character and is used for checking the DFA second state. If the character passed is equal to ‘E’ or ‘e’ then we go to third state else to the beginning state i.e 0.

void secondState(char c){
   if (c == 'e' || c == 'E')
      dfa = 3;
   else
      dfa = 0;
}

The thirdState(char c) takes a character and is used for checking the DFA third state. If the character equals ‘t’ or ‘T’ then we go to first state (1) else to the starting state i.e 0.

void thirdState(char c){
   if (c == 't' || c == 'T')
      dfa = 1;
   else
      dfa = 0;
}

The isAccepted(string str) takes the string to be tested as parameter. The len variable stores the length of the string. The for loop iterates till the string length. If the dfa = 0 then the start(char c) function is called, if the dfa equals 1 then firstState(char c) function is called and if the dfa equals 2 then secondState(char c) function is called and if dfa isn’t 1,2,3 then the thirdState(char c) function is called. We return true or false based upon if the dfa is 3 or not. If the dfa isn’t equal to three then the string is accepted otherwise not.

bool isAccepted(string str){
   int len = str.length();
   for (int i=0; i < len; i++) {
      if (dfa == 0)
         begin(str[i]);
      else if (dfa == 1)
         firstState(str[i]);
      else if (dfa == 2)
         secondState(str[i]);
      else
         thirdState(str[i]);
   }
   return (dfa != 3);
}

Example

Let us look at the following implementation to find the DFA for strings not ending with “THE” −

 Live Demo

#include <iostream>
#include <string>
using namespace std;
int dfa = 0;
void begin(char c){
   if (c == 't' || c == 'T')
      dfa = 1;
}
void firstState(char c){
   if (c == 't' || c == 'T')
      dfa = 1;
   else if (c == 'h' || c == 'H')
      dfa = 2;
   else
      dfa = 0;
}
void secondState(char c){
   if (c == 'e' || c == 'E')
      dfa = 3;
   else
      dfa = 0;
}
void thirdState(char c){
   if (c == 't' || c == 'T')
      dfa = 1;
   else
      dfa = 0;
}
bool isAccepted(string str){
   int len = str.length();
   for (int i=0; i < len; i++) {
      if (dfa == 0)
         begin(str[i]);
      else if (dfa == 1)
         firstState(str[i]);
      else if (dfa == 2)
         secondState(str[i]);
      else
         thirdState(str[i]);
   }
   return (dfa != 3);
}
int main(){
   string str = "helloForTheWorld";
   if (isAccepted(str) == true)
      cout<<"The string "<<str<<" is accepted ";
   else
      cout<<"The string "<<str<<" is not accepted";
   return 0;
}

Output

The above code will produce the following output −

The string helloForTheWorld is accepted

Updated on: 16-Jan-2021

201 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements