## Convert DFA to Equivalent C/C++ Or Java Code

Posted 2 years ago

Last Updated 1 year ago

5853 views ## Deterministic Finite Automaton

A deterministic finite automaton (DFA) also known as deterministic finite state machine , is a finite state machine that accepts/rejects finite strings of symbols and only produces a unique computation (or run) of the automaton for each input string. Source: Wikipedia

## Conversion to C/C++ or Java Code

we can easily convert any DFA to Code , here is the machine that will accept string that contain "nano" sub string and will reject all other strings Java Code

```package dfatojavacode;

/**
*
* @author Azeem Tariq
*/
public class DFATOJavaCode {

public static void main(String[] args) {

System.out.println("Enter Strig to Run on DFA :");
int state = 1;
for (char c : string.toCharArray()) {
switch (state) {
case (1): {
if (c == 'n' || c == 'N') {
state = 2;
}

}
break;
case (2): {
if (c == 'a' || c == 'A') {
state = 3;
} else if (c == 'n' || c == 'N') {
state = 2;
}
}
break;
case (3): {
if (c == 'n' || c == 'N') {
state = 4;
} else {
state = 1;
}
}
break;
case (4): {
if (c == 'o' || c == 'O') {
state = 5;
} else if (c == 'n' || c == 'N') {
state = 2;
} else {
state = 1;
}
}
break;
case (5): {
state = 5;
}
break;
}
}

if (state == 5) {
System.out.println("DFA accepts this string");
} else {
System.out.println("DFA does not accepts this string");
}

}

}
```

### C/C++ Code

```#include
#include
using namespace std;
int main()
{
string str ;
cout<<"Enter String to Test: "<>str;
int state=1; // start state
for(int i=0;i```

if you guys have any problem then comment below i will try to help you .

#### Enter Email to Subscribe

Amazing tutorials deliver daily to you