Home >Backend Development >C++ >Program to build a DFA starting with 'a' and ending with 'a' from input

Program to build a DFA starting with 'a' and ending with 'a' from input

王林
王林forward
2023-09-06 21:37:141399browse

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.

Example

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] == &#39;a&#39; && str[lenght-1] == &#39;a&#39;){
      printf("Accepted");
      else{
         printf("Rejected");
         return 0;
      }
   }
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete