Experiment 1
Experiment 1
1. Design a Program for creating machine that accepts three consecutive one.
#include <iostream>
#include <string>
enum State {
START,
ONE,
TWO,
THREE,
ACCEPT
};
switch (currentState) {
case START:
if (ch == '1') {
currentState = ONE;
break;
case ONE:
if (ch == '1') {
currentState = TWO;
} else {
currentState = START;
break;
case TWO:
if (ch == '1') {
currentState = THREE;
} else {
currentState = START;
break;
case THREE:
if (ch == '1') {
currentState = ACCEPT;
} else {
currentState = START;
break;
case ACCEPT:
return true;
}
}
int main() {
std::string input;
if (hasThreeConsecutiveOnes(input)) {
} else {
std::cout << "The string does not contain three consecutive '1's.\n";
return 0;
}
EXPERIMENT:-2
2. Design a Program for creating machine that accepts the string always ending with 101.
#include <iostream>
#include <string>
// Check if the string has at least 3 characters and ends with "101"
if (str.size() >= 3) {
return false;
int main() {
string input;
cout << "Invalid input! Please enter a binary string." << endl;
return 1;
}
// Check if the string ends with "101"
if (endsWith101(input)) {
Input:
110101
Output:
#include <iostream>
#include <string>
enum State {
START,
S1,
S2,
S3
};
// Function to check if the input string ends with "101" using a Mode 3 FSM
switch (currentState) {
case START:
break;
case S1:
currentState = (c == '0') ? S2 : S1;
break;
case S2:
break;
case S3:
break;
int main() {
string input;
if (isMode3Match(input)) {
cout << "The string is accepted by the Mode 3 machine (ends with '101')." << endl;
} else {
cout << "The string is not accepted by the Mode 3 machine." << endl;
return 0;
}
EXPERIMENT:-4
#include <iostream>
#include <string>
if (!isdigit(c)) {
return (lastDigit == '0' || lastDigit == '2' || lastDigit == '4' || lastDigit == '6' || lastDigit == '8');
int main() {
string input;
if (isDivisibleBy2(input)) {
} else {
cout << "The number is not divisible by 2." << endl;
return 0;
Input:
124
Output:
5. Design a program for creating a machine which accepts string having equal no. of 1’s and 0’s.
#include <iostream>
#include <string>
int countOnes = 0;
int countZeros = 0;
if (c == '1') {
countOnes++;
} else if (c == '0') {
countZeros++;
} else {
return false;
int main() {
string input;
cout << "Enter a binary string: ";
if (hasE…
EXPERIMENT:-6
6. Design a program for creating a machine which count number of 1’s and 0’s in a given string.
#include <iostream>
#include <string>
if (c == '1') {
ones++;
} else if (c == '0') {
zeros++;
int main() {
string input;
countOnesAndZeros(input);
return 0;
Output :
Number of 1's: 5
Number of 0's: 4
EXPERIMENT:-7
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
class NDFA {
public:
};
class DFA {
public:
map<set<int>, int> stateMap; // maps the set of NDFA states to a unique DFA state index
};
queue<int> q;
q.push(state);
while (!q.empty()) {
q.pop();
if (closure.find(nextState) == closure.end()) {
closure.insert(nextState);
q.push(nextState);
return closure;
DFA dfa;
queue<set<int>> q;
q.push(startStateClosure);
dfa.stateMap[startStateClosure] = 0;
dfa.startState = 0;
while (!q.empty()) {
q.pop();
// Check if the current set of states contains any accepting state of the NDFA
if (ndfa.acceptingStates.find(state) != ndfa.acceptingStates.end()) {
dfa.acceptingStates.insert(dfa.stateMap[currentState]);
break;
set<int> nextState;
nextState.insert(ndfa.transitions[state].begin(), ndfa.transitions[state].end());
}
if (dfa.stateMap.find(nextState) == dfa.stateMap.end()) {
dfa.stateMap[nextState] = dfa.transitions.size();
q.push(nextState);
dfa.transitions[dfa.stateMap[currentState]][symbol] = dfa.stateMap[nextState];
return dfa;
cout << "} -> State " << state.second << endl;
}
cout << "\nDFA Transitions: " << endl;
…
EXPERIMENT:-8
8. Design a Program to create PDA machine that accept the well-formed parenthesis.
#include <iostream>
#include <stack>
#include <string>
stack<char> s;
if (ch == '(') {
s.push(ch);
if (s.empty()) {
return false;
else {
return false;
return s.empty();
int main() {
string input;
if (isValidParentheses(input)) {
cout << "The input string has well-formed parentheses." << endl;
} else {
cout << "The input string does not have well-formed parentheses." << endl;
return 0;
Input :
Output :