Professional Documents
Culture Documents
Assignment # 01 2273107 20-10-2022
Assignment # 01 2273107 20-10-2022
Total Marks:
Obtained Marks:
Date:
Instructions: Copied or shown assignments will be marked zero. Late submissions are not
entertained in any case.
Question
Consider a language defined over ∑={a,b} that accepts the strings starting with a.
a. Give its transition diagram using JFlap.
b. Write a C/C++ program that stays in an infinite loop, prompts the user for a string, terminates
if the string is QUIT, and otherwise implements the DFA using the scheme that allows state
to state function-call and recursion.
c. Give the source code and the runtime screen while testing the strings aabab, abbaaba and
abbb.
Note:
1. Change the filename to your ID, e.g. 2073105.doc
2. Upload the .doc on Google Classroom.
3. Make sure that the output screen does not have black background.
4. Poor indentation and wrong format will result in deduction of marks.
Solution-
Step-01:
Step-02:
We will construct DFA for the following strings-
a
aa
Step-03:
The required DFA is-
#include <iostream>
#include <string>
using namespace std;
int ctr;
char ch;
string str;
void state1();
void state2();
void state3();
int main()
{
do {
cout << "DFA: Strings starting with a" << endl;
cout << "String: ";
cin >> str;
if( str!="quit" ) {
ctr = 0;
state1();
cout << endl;
}
} while( str!="quit" );
return 0;
}
void state1()
{
if( ctr>=str.length() ) {
cout << "String Rejected" << endl;
return;
}
ch = str.at(ctr++);
if( ch=='a' ) state2();
else if( ch=='b' ) state3();
else {
cout << "Invaid character " << ch << "in the input string" << endl;
return;
}
}
void state2()
{
if( ctr>=str.length() ) {
cout << "String Accepted" << endl;
return;
}
ch = str.at(ctr++);
if( ch=='a' || ch=='b' )
state2();
else {
cout << "Invaid character " << ch << "in the input string" << endl;
return;
}
}
FOP MSCS-1 SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
void state3()
{
if( ctr>=str.length() ) {
cout << "String Rejected" << endl;
return;
}
ch = str.at(ctr++);
if( ch=='a' || ch=='b' )
state3();
else {
cout << "Invaid character " << ch << " in the input string" <<
endl;
return;
}
}
Screen Shot
//
// main.cpp
// DFA
//
//
#include <iostream>
#include <string.h>
if(index == input.length())
return false;
if(input[index] == 'a')
else
if(index == input.length())
return false;
if(input[index] == 'a')
else
if(index == input.length())
if(input[index] == 'a')
else
if(index == input.length())
return true;
if(input[index] == 'a')
else
if(index == input.length())
return false;
if(input[index] == 'a')
else
string input;
while(1)
cin>>input;
if(input == "quit")
exit(1);
if(result)
cout<<"String accepted"<<endl;
else
return 0;