-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstring_contains_aba_substring.cpp
53 lines (46 loc) · 1.47 KB
/
string_contains_aba_substring.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// cpp program for DFA for the language of string over {a,b} such that each string contain aba as substring
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str; // string to be checked
char state = 0; // initial state (q0)
cout << "Enter the string: ";
cin >> str;
// loop to check each character of the string for the DFA
for (int i = 0; i < str.length(); i++)
{
// check if the string is over {a,b} or not
if (str[i] != 'a' && str[i] != 'b')
{
cout << "String not accepted.\nPlease enter a string over {a,b}" << endl;
return 0;
}
// dfa transition check
if (state == 0 && str[i] == 'a')
state = 1;
else if (state == 0 && str[i] == 'b')
state = 0;
else if (state == 1 && str[i] == 'a')
state = 1;
else if (state == 1 && str[i] == 'b')
state = 2;
else if (state == 2 && str[i] == 'a')
state = 3;
else if (state == 2 && str[i] == 'b')
state = 0;
else if (state == 3 && str[i] == 'a')
state = 3;
else if (state == 3 && str[i] == 'b')
state = 3;
}
// check if the string is accepted or not,
// i.e. if the final state is 3 then string is accepted
// else string is not accepted
if (state == 3)
cout << "String accepted";
else
cout << "String not accepted";
return 0;
}