TOC Practical File1.
TOC Practical File1.
TOC Practical File1.
int main() {
string input;
cout << "Enter a binary string: ";
cin >> input;
if (hasThreeConsecutiveOnes(input)) {
cout << "The string has three consecutive '1's as a
substring." << endl;
} else {
cout << "The string does not have three consecutive
'1's as a substring." << endl;
}
return 0;
}
OUTPUT:
CODE:
#include <iostream>
#include <string>
enum State { A, B, C, D, E };
State state = A;
for (char ch : input) {
switch (state) {
case A:
if (ch == '0') {
state = A; // Stay in A
} else if (ch == '1') {
state = B; // Move to B
}
break;
case B:
if (ch == '0') {
state = B; // Stay in B
} else if (ch == '1') {
state = C; // Move to C
}
break;
case C:
if (ch == '0') {
state = B; // Return to B
} else if (ch == '1') {
state = D; // Move to D
}
break;
case D:
state = E;
break;
case E:
break;
}
}
int main() {
string inputString;
if (simulateDFA(inputString)) {
cout << "Valid String: Contains exactly two or three
1's" << endl;
} else {
cout << "Invalid String: Does not contain exactly two
or three 1's" << endl;
}
return 0;
}
OUTPUT:
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
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
cout << "Enter a string over {a, b}: ";
cin >> input;
if (isAcceptedByFA(input)) {
cout << "The string is accepted by the FA." << endl;
} else {
cout << "The string is not accepted by the FA." <<
endl;
}
return 0;
}
case 1:
if (c == 'a' || c == 'b') {
state = 2;
} else {
return false;
}
break;
case 2:
if (c == 'a' || c == 'b') {
state = 2;
} else {
return false;
}
break;
default:
return false;
}
}
int main() {
string input;
if (simulateFA(input)) {
cout << "The string is accepted by the FA.\n";
} else {
cout << "The string is not accepted by the FA.\n";
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int state = 0;
case 1:
if (c == 'a') {
state = 0;
} else if (c == 'b') {
state = 3;
} else {
return false;
}
break;
case 2:
if (c == 'a') {
state = 3;
} else if (c == 'b') {
state = 0;
} else {
return false;
}
break;
case 3:
if (c == 'a') {
state = 2;
} else if (c == 'b') {
state = 1;
} else {
return false;
}
break;
default:
return false;
}
}
return state == 0;
}
int main() {
string input;
if (simulateFA(input)) {
cout << "The string is accepted by the FA.\n";
} else {
cout << "The string is not accepted by the FA.\n";
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
cout << "Enter a string: ";
cin >> input;
if (simulateUnion(input)) {
cout << "The string is accepted by L1 ? L2.\n";
} else {
cout << "The string is not accepted by L1 ? L2.\n";
}
return 0;
}
b. Intersection of the languages L1 and L2
int main() {
string input;
cout << "Enter a string: ";
cin >> input;
if (simulateIntersection(input)) {
cout << "The string is accepted by L1 n L2.\n";
} else {
cout << "The string is not accepted by L1 n L2.\n";
}
return 0;
}
c. Language L1 L2 (concatenation)
int main() {
string input;
cout << "Enter a string: ";
cin >> input;
if (simulateConcatenation(input)) {
cout << "The string is accepted by L1L2
(concatenation).\n";
} else {
cout << "The string is not accepted by L1L2
(concatenation).\n";
}
return 0;
}
7. Design a PDA and write a program for simulating
the machine which accepts the language {anbn where
n>0, S= {a, b}}.
#include <iostream>
#include <stack>
#include <string>
int state = 0;
case 1:
if (symbol == 'a') {
pdaStack.push('A');
} else if (symbol == 'b') {
if (!pdaStack.empty() && pdaStack.top() ==
'A') {
pdaStack.pop();
state = 2;
} else {
return false;
}
} else {
return false;
}
break;
case 2:
if (symbol == 'b') {
if (!pdaStack.empty() && pdaStack.top() ==
'A') {
pdaStack.pop();
} else {
return false;
}
} else {
return false;
}
break;
default:
return false;
}
}
symbol Z
return state == 2 && pdaStack.size() == 1 &&
pdaStack.top() == 'Z';
}
int main() {
string input;
return 0;
}
#include <iostream>
#include <stack>
#include <string>
case 1:
if (symbol == 'a' || symbol == 'b') {
if (!pdaStack.empty() && pdaStack.top() ==
symbol) {
pdaStack.pop();
} else {
return false;
}
} else {
return false;
}
break;
default:
return false;
}
}
return state == 1 && pdaStack.size() == 1 &&
pdaStack.top() == 'Z';
}
int main() {
string input;
if (simulatePDA(input)) {
cout << "The string is accepted by the PDA." << endl;
} else {
cout << "The string is rejected by the PDA." << endl;
}
return 0;
}
9. Design and simulate a Turing Machine that accepts
the language a^n b^n c^n where n >0.
#include <iostream>
#include <string>
using namespace std;
string tape;
int head;
string state;
bool simulateTM() {
head = 0;
state = "q0";
while (true) {
if (state == "q0") {
if (tape[head] == 'a') {
tape[head] = 'X';
head++;
state = "q1";
} else if (tape[head] == 'Y' || tape[head] == 'Z' ||
tape[head] == '\0') {
state = "reject";
}
} else if (state == "q1") {
if (tape[head] == 'X' || tape[head] == 'Y') {
head++;
} else if (tape[head] == 'b') {
tape[head] = 'Y';
head++;
state = "q2";
} else if (tape[head] == 'c' || tape[head] == '\0') {
state = "reject";
}
} else if (state == "q2") {
if (tape[head] == 'X' || tape[head] == 'Y' ||
tape[head] == 'Z') {
head++;
} else if (tape[head] == 'c') {
tape[head] = 'Z'; // Mark 'c' as processed
head--; // Move left
state = "q3"; // Transition to q3
} else if (tape[head] == '\0') {
state = "reject"; // Missing 'c'
}
} else if (state == "q3") {
if (tape[head] == 'X' || tape[head] == '\0') {
state = "q4"; // Check if all symbols are
processed
} else {
head--; // Move left to find the next unprocessed
symbol
}
} else if (state == "q4") {
return true; // Accept the input
} else if (state == "reject") {
return false; // Reject the input
}
}
}
int main() {
cout << "Enter the input string (format: a^n b^n c^n): ";
cin >> tape;
if (simulateTM()) {
cout << "The input string is ACCEPTED by the Turing
Machine." << endl;
} else {
cout << "The input string is REJECTED by the Turing
Machine." << endl;
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
while (true) {
if (state == "q0") {
if (tape[head] == '0') {
tape[head] = '1'; // Replace '0' with '1'
state = "q2"; // Transition to termination state
} else if (tape[head] == '1') {
tape[head] = '0'; // Carry over by replacing '1'
with '0'
head--; // Move left
} else if (head < 0) { // If all digits are processed
state = "q3"; // Overflow case
}
} else if (state == "q2") {
state = "q_accept"; // Accept the input after
successful increment
} else if (state == "q3") {
tape = "1" + tape; // Add '1' at the leftmost position
state = "q_accept";
} else if (state == "q_accept") {
break; // Exit simulation
}
}
}
int main() {
cout << "Enter the binary number to increment: ";
cin >> tape;
simulateTM();
cout << "Binary number after increment: " << tape <<
endl;
return 0;
}