- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements