Toc Prac
Toc Prac
PRACTICAL FILE
NAME : POOJA SONI
ROLL NO. 22/CS/48
COURSE : B.SC.(H) COMPUTER SCIENCE
SUBMITTED TO : PROF. HARENDRA PRATAP SINGH
Question 1 ) Design a Finite Automata (FA) that accepts all strings over S={0, 1} having three consecutive 1's as
a substring. Write a program to simulate this FA.
#include <iostream>
#include <string>
using namespace std;
if (w[i] == '0') {
State0(w, i + 1); // Stay in State 0
} else if (w[i] == '1') {
State1(w, i + 1); // Transition to State 1
}
}
if (w[i] == '0') {
State0(w, i + 1); // Transition back to State 0
} else if (w[i] == '1') {
State2(w, i + 1); // Transition to State 2
}
}
if (w[i] == '0') {
State0(w, i + 1); // Transition back to State 0
} else if (w[i] == '1') {
State3(w, i + 1); // Transition to State 3 (accepting state)
}
}
void State3(const string& w, int i) {
if (i == w.length()) {
cout << "String is accepted" << endl; // End in accepting state
return;
}
Output :
Enter a string of '0's and '1's: 01101000
String is rejected
Question 2) Design a Finite Automata (FA) that accepts all strings over S={0, 1} having either exactly two 1's or
exactly three 1's, not more nor less. Write a program to simulate this FA.
Code :
#include <iostream>
#include <string>
using namespace std;
int main() {
string w;
cout << "Enter a string of '0's and '1's: ";
cin >> w;
State0(w, 0);
return 0;
}
if (w[i] == '0') {
State0(w, i + 1); // Stay in State 0
} else if (w[i] == '1') {
State1(w, i + 1); // Transition to State 1
}
}
if (w[i] == '0') {
State1(w, i + 1); // Stay in State 1
} else if (w[i] == '1') {
State2(w, i + 1); // Transition to State 2
}
}
if (w[i] == '0') {
State2(w, i + 1); // Stay in State 2
} else if (w[i] == '1') {
State3(w, i + 1); // Transition to State 3
}
}
if (w[i] == '0') {
State3(w, i + 1); // Stay in State 3
} else if (w[i] == '1') {
State4(w, i + 1); // Transition to dead state
}
}
Output :
Enter a string of '0's and '1's: 0111
String is accepted.
Question 3 )Design a Finite Automata (FA) that accepts language L1, over S={a, b}, comprising of all strings (of
length 4 or more) having first two characters same as the last two. Write a program to simulate this
FA.
Code :
#include <iostream>
#include <string>
using namespace std;
int main() {
string w;
cout << "Enter a string of 'a's and 'b's: ";
cin >> w;
checkString(w);
return 0;
}
Output :
Enter a string of 'a's and 'b's: abaaabbabab
String is rejected
Enter a string of 'a's and 'b's: baaaababbbabab
String is accepted
Question 4). Design a Finite Automata (FA) that accepts language L2, over S= {a, b} where L2= a(a+b)*b. Write
a program to simulate this FA.
Code :
#include <iostream>
#include <string>
using namespace std;
int main() {
string w;
cout << "Enter a string of 'a's and 'b's: ";
cin >> w;
State0(w, 0);
return 0;
}
if (w[i] == 'a') {
State1(w, i + 1); // Move to State 1
} else {
cout << "String is rejected" << endl; // Can't start with 'b'
}
}
if (w[i] == 'a') {
State1(w, i + 1); // Stay in State 1
} else if (w[i] == 'b') {
State2(w, i + 1); // Move to State 2
}
}
if (w[i] == 'a') {
State1(w, i + 1); // Move to State 1
} else if (w[i] == 'b') {
State2(w, i + 1); // Stay in State 2
}
}
Output :
Enter a string of 'a's and 'b's: aaabbbbababb
String is accepted
Enter a string of 'a's and 'b's: ab
String is accepted
Question 5.) Design a Finite Automata (FA) that accepts language EVEN-EVEN over S={a, b}. Write a program
to simulate this FA
Code :
#include <iostream>
#include <string>
using namespace std;
int main() {
string w;
cout << "Enter a string of 'a's and 'b's: ";
cin >> w;
State0(w, 0);
return 0;
}
if (w[i] == 'a') {
State1(w, i + 1); // Move to State 1
} else if (w[i] == 'b') {
State2(w, i + 1); // Move to State 2
} else {
cout << "String is rejected" << endl; // Invalid character
}
}
if (w[i] == 'a') {
State0(w, i + 1); // Move to State 0
} else if (w[i] == 'b') {
State3(w, i + 1); // Move to State 3
} else {
cout << "String is rejected" << endl; // Invalid character
}
}
if (w[i] == 'a') {
State3(w, i + 1); // Move to State 3
} else if (w[i] == 'b') {
State0(w, i + 1); // Move to State 0
} else {
cout << "String is rejected" << endl; // Invalid character
}
}
Output :
Enter a string of 'a's and 'b's: aabaaa
String is rejected
Enter a string of 'a's and 'b's: aabbaa
String is accepted
code :
#include <iostream>
#include <string>
#include <regex>
int main() {
string pattern1, pattern2, w;
return 0;
}
Output :
Enter a regular expression for Language L1: (ab)*
Enter a regular expression for Language L2: (a|ab)*
Enter a string to test: abab
Union of L1 and L2: Accepted
Intersection of L1 and L2: Accepted
Concatenation of L1 and L2: Accepted
Code :
#include <iostream>
#include <stack>
#include <string>
using namespace std;
// Start in state q0
int state = 0;
int main() {
string input;
cout << "Enter a string to test (only a's and b's): ";
getline(cin, input);
if (isAccepted(input)) {
cout << "Accepted\n";
} else {
cout << "Rejected\n";
}
return 0;
}
Output :
Enter a string to test (only a's and b's): aaabbb
Accepted
Question 8). Design a PDA and write a program for simulating the machine which accepts the language {wXwr| w is
any string over S={a, b} and wr is reverse of that string and X is a special symbol }.
Code :
#include <iostream>
#include <stack>
#include <string>
using namespace std;
bool isAccepted(const string& input) {
stack<char> pdaStack;
// Start in state q0
int state = 0;
int main() {
string input;
cout << "Enter a string to test (format wXwR where w is over {a,b}): ";
getline(cin, input);
if (isAccepted(input)) {
cout << "Accepted\n";
} else {
cout << "Rejected\n";
}
return 0;
}
Output :
Enter a string to test (format wXwR where w is over {a,b}): ababbXbbaba
Accepted
Question 9.) Design and simulate a Turing Machine that accepts the language anbncn where n >0.
Code :
Question 10.) Design and simulate a Turing Machine which will increment the given binary number by 1.
Code :
#include <iostream>
#include <string>
using namespace std;
class TuringMachine {
public:
TuringMachine(const string& input) : tape(input + ' '), head(0) {}
void increment() {
// Move to the end of the number
while (tape[head] != ' ') {
head++;
}
head--; // Move back to the last digit
// If we exit the loop and head is out of bounds, we need to add a new '1'
if (head < 0) {
tape = '1' + tape; // Prepend '1' to the tape
}
}
private:
string tape;
int head;
};
int main() {
string input;
cout << "Enter a binary number: ";
getline(cin, input);
// Validate input
if (input.empty() || input.find_first_not_of("01") != string::npos) {
cout << "Invalid input. Please enter a binary number.\n";
return 1;
}
TuringMachine tm(input);
tm.increment();
cout << "Incremented binary number: " << tm.getResult() << endl;
return 0;
}
Output :
Enter a binary number: 000110000101
Incremented binary number: 000110000110
THANK YOU