Program to build DFA that starts and ends with ‘a’ from the input (a, b)


DFA stands for Deterministic Finite Automata. It is a finite state machine that accepts or a string based on its acceptor.

Here, we are going to make a DFA that accepts a string that starts and ends with a. The input is from the set (a,b). Based on this we will design a DFA. Now, Let's discuss some valid and invalid cases that are accepted by a DFA.

Strings that are accepted by DFA: ababba, aabba, aa, a.

Strings that are not accepted by DFA: ab, b, aabab.

Example

This program check for a string that starts and ends with a. This DFA will accept all strings that starts and ends with a. The code checks for first and last elements equality and rest everything in between is possible from (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;
      }
   }
}

Output

Accepted

Updated on: 06-Aug-2019

499 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements