Home >Backend Development >C++ >Program to build a DFA starting with 'a' and ending with 'a' from input
DFA stands for Deterministic Finite Automata. It is a finite state machine that accepts or rejects a string based on its receiver.
Here we will make a DFA that accepts strings starting with a and ending with a. The input comes from the set (a,b). Based on this, we will design a DFA. Now, let's discuss some valid and invalid situations that DFA accepts.
DFA accepted strings: ababba, aabba, aa, a.
Strings not accepted by DFA: ab, b, aabab.
This program checks for strings that start with a and end with a. This DFA will accept all strings starting with a and ending with a. The code checks the equality of the first and last elements, and all elements in between can be any characters in (a,b).
#include <iostream> #include <string.h> using namespace std; int main(){ char str[] = {"ababba"}; int lenght = strlen(str); if(str[0] == 'a' && str[lenght-1] == 'a'){ printf("Accepted"); else{ printf("Rejected"); return 0; } } }
Accepted
The above is the detailed content of Program to build a DFA starting with 'a' and ending with 'a' from input. For more information, please follow other related articles on the PHP Chinese website!