-
Notifications
You must be signed in to change notification settings - Fork 0
/
question_5.cpp
48 lines (39 loc) · 1.39 KB
/
question_5.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
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
bool DFA_endingwith_01(char[]);
enum states{q0_starting,q1,q2_final,Rejected};
int main()
{
int i;char input[100];string choice;
do{
cout<<"Consider that this program is only for input symbols {0,1} with ending as 01"<<endl;
cout<<"Enter string :";
cin>>input;
if (DFA_endingwith_01(input)) {cout<<"String is accepted"<<endl;}
else{cout<<"String is rejected"<<endl;}
cout<<"do you want to continue[y/n]"; cin>>choice;
}while(choice != "n");
return 0;
}
bool DFA_endingwith_01(char w[])
{
string check_value;int state = q0_starting;
for(int i = 0;i<strlen(w);i++)
{
check_value = w[i];
if (check_value != "1" && check_value != "0"){cout<<"Please enter valid string"<<endl;state = Rejected;break;}
else
{
if(state == q0_starting && check_value == "0") {state = q1;}
else if(state == q0_starting && check_value == "1") {state = q0_starting;}
else if(state == q1 && check_value == "0"){state = q1;}
else if(state == q1 && check_value == "1"){state = q2_final;}
else if(state == q2_final && check_value == "0"){state = q1;}
else if(state == q2_final && check_value == "1"){state = q0_starting;}
}
}
if (state == q2_final){return true;}
else {return false;}
}