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

王林
Release: 2023-09-06 21:37:14
forward
1409 people have browsed it

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;
      }
   }
}
Copy after login

Output

Accepted
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template