RUSTAMJI INSTITUTE OF TECHNOLOGY
BSF ACADEMY, TEKANPUR
Lab File for
CS501 (TOC Lab Manual)
Submitted by
Shweta Shukla(0902CS221059)
B.Tech. Computer Science & Engineering 5th Semester
(2022-2026 batch)
Subject Teacher File Checked by
Dr. Yograj Sharma Mr. Yashwant Pathak
1|0902CS221059
Self-Declaration Certificate
I, Shweta Shukla, hereby declare that I have completed the lab work of
CS501(TOC) at my own effort and understanding.
I affirm that the work submitted is my own, and I take full responsibility for its
authenticity and originality.
Date: Shweta Shukla
0902CS221059
2|0902CS221059
INDEX
Sno Name of Experiment Date Page Signature
. no.
1. Design a Program for creating machine that 17/9/24
accepts three consecutive one.
2. 24/9/24
Design a Program for creating machine that
accepts the string always ending with 101.
3. 1/10/24
Design a Program for Mode 3 Machine
4. 8/10/24
Design a program for accepting decimal
number divisible by 2.
5. 15/10/2
Design a program for creating a machine
4
which accepts string having equal no. of 1’s
and 0’s and no. of 1<=3.
6. 22/10/2
Design a program for creating a machine
4
which counts number of 1’s and 0’s in a
given string.
7. 05/11/2
Design a Program to find 2’s complement of
4
a given binary number.
8. Design a Program which will increment the 12/11/2
given binary number by 1. 4
9. 19/11/2
Design a PDA to accept WCWR where w
4
is any string and WR is reverse of that
string and C is a Special symbol.
10. Design a Turing machine that’s accepts the 26/11/2
following language an bn cn where n>0. 4
3|0902CS221059
EXPERIMENT:1
Objective: Design a Program for creating machine that accepts three consecutive one.
Graph Representation:
Program:
#include <bits/stdc++.h>
using namespace std;
// Returns true if s1 is substring of s2
int isSubstring(string s1,string s2)
{
int M = s1.length();
int N=s2.length();
int res;
/* A loop to slide pat[] one by one */
for(int i =0; i <=N-M; i++)
{ int j;
/*For current index i, check for pattern match*/
for(j=0; j <M; j++)
if (s2[i + j] != s1[j])
break;
if (j == M)
{res = i;
break;
else{
res=-1;
4|0902CS221059
if(res!=-1)
cout<<s2<<" is Accepted\n";
else
cout<<s2<<"is Rejected\n";
/* Driver program to test above function */
int main()
{
string s1 ="111";
string s2 = "00010101110";
string s3="111";
string s4 = "000110";
isSubstring(s1, s2);
isSubstring(s1, s3);
isSubstring(s1, s4);
return0;
}
5|0902CS221059
EXPERIMENT:2
Objective: Design a Program for creating machine that accepts the string always ending with 101.
Graphical Representation:
Program:
#include <bits/stdc++.h>
using namespace std;
int endsWith(string s1,string s2){
int M =s1.length();
int N=s2.length();
int res;
/* A loop to slide pat[] one by one */
for(int i =0; i <=N-M; i++)
{ int j;
/*For current index i, check for pattern match*/
for(j=0; j <M; j++)
if (s2[i + j] != s1[j])
break;
if (j == M)
{res = i;
break;
}
else
res=-1;
6|0902CS221059
}
if(res!=-1)
cout<<s2<<" is Accepted\n";
else
cout<<s2<<"is Rejected\n";
/* Driver program to test above function */
int main()
{
string s1 ="111";
string s2 = "00010101110";
string s3="111";
string s4 = "000110";
isSubstring(s1, s2);
isSubstring(s1, s3);
isSubstring(s1, s4);
return0;
}
7|0902CS221059
EXPERIMENT:3
Objective: Design a Program for Mode 3 Machine.
Graphical Representation:
Program:
#include <bits/stdc++.h>
using namespace std;
int mod3(string s1){
int M = s1.length();
if(M%3){
cout<<s1<<"is Rejected\n";
}
else{
cout<<s1<<" is Accepted\n";
}
/* Driver program to test above function */ int
main()
{
string s1 = "aba";
string s2 = "abaa";
string s3 = "ababaaa";
string s4 = "ababaa";
mod3(s1);
mod3(s2);
mod3(s3);
mod3(s4);
return 0;
8|0902CS221059
}
9|0902CS221059
EXPERIMENT:4
Objective: Design a program for accepting decimal number divisible by 2.
Graphical Representaion:
Program:
#include <bits/stdc++.h>
using namespace std;
int mod2(int num)
{
if(num%2){
cout<<num<<"is Rejected\n";
}
else{
cout<<num<<"is Accepted\n";
}
}
/* Driver program to test above function */
int main()
{
int a = 5, b = 2, c = 0, d = 4, e = 7;
mod2(a);
mod2(b);
mod2(c);
mod2(d);
mod2(e);
return0;
10 | 0 9 0 2 C S 2 2 1 0 5 9
}
11 | 0 9 0 2 C S 2 2 1 0 5 9
EXPERIMENT:5
Objective: Design a program for creating a machine which accepts string having equal no.
of 1’s and 0’s and no. of 1<=3.
Graphical Representation:
Program:
#include <bits/stdc+
+.h> using namespace
std;
int equalZO(strings1)
{
int count_one = 0, count_zero = 0;
count_one=count(s1.begin(),s1.end(),'1');
count_zero = count(s1.begin(),s1.end(),'0');
if(count_one ==count_zero)
cout<<s1<<" is Accepted\n";
else
cout<<s1<<"is Rejected\n";
}
int main()
{
string s1 = "010";
string s2 = "1010";
12 | 0 9 0 2 C S 2 2 1 0 5 9
string s3 = "1100";
string s4 = "00011";
equalZO(s1);
equalZO(s2);
equalZO(s3);
equalZO(s4);
return 0;
13 | 0 9 0 2 C S 2 2 1 0 5 9
EXPERIMENT:6
Objective: Design a program for creating a machine which counts number of 1’s and 0’s in
a given string.
Graphical Representation:
Program:
#include <bits/stdc++.h>
using namespace std;
int countZO(strings1)
{
int count_one = 0, count_zero = 0;
count_one=count(s1.begin(),s1.end(),'1');
count_zero=count(s1.begin(),s1.end(),'0');
cout <<"Number of 1's:" <<count_one <<" and 0's:" <<count_zero <<" in" <<s1 <<"\n";
}
int main()
{
string s1 = "010";
string s2 = "1010";
string s3 = "1100";
string s4 = "00011";
countZO(s1);
countZO(s2);
14 | 0 9 0 2 C S 2 2 1 0 5 9
countZO(s3);
countZO(s4);
return 0;
}
15 | 0 9 0 2 C S 2 2 1 0 5 9
EXPERIMENT:7
Objective: Design a Program to find 2’s complement of a given binary number.
Graphical Representation:
Program:
#include<bits/stdc++.h>
using namespace std;
// Function to find two's complement
string findTwoscomplement(string
str)
{
int n = str.length();
int i;
for(i =n-1 ; i >=0 ; i--)
if (str[i] == '1')
break;
if(i==-1)
return '1'+str;
//Continue traversal after the position of
//first '1'
for(int k =i-1 ; k >=0; k--)
{
//Just flip the
valuesif
(str[k]=='1')
str[k] = '0';
else
str[k]='1';
}
return str;;
16 | 0 9 0 2 C S 2 2 1 0 5 9
int main()
{
string str1 = "00000101";
string str2 = "00101011";
string str3="101011";
cout << findTwoscomplement(str1) << "\
n"; cout << findTwoscomplement(str2) <<
"\n"; cout << findTwoscomplement(str3)
<< "\n"; return 0;
}
17 | 0 9 0 2 C S 2 2 1 0 5 9
EXPERIMENT:8
Objective: Design a Program which will increment the given binary number by1.
Graphical Representation:
Program:
#include <iostream>
using namespace std;
int main()
{
int len,i;
string n,comp;
bool firstone=false;
cout<<"ENTER BINARY NUMBER: ";
cin>>n;
len=n.length();
comp.resize(len);
for( i=(len-1) ; i>=0 ; i-- )
{
if(firstone==false)
{
if(n[i]=='1')
{
comp[i]='0';
}
else
{
comp[i]='1';
firstone=true;
}
}
else
{
comp[i]=n[i];
}
}
18 | 0 9 0 2 C S 2 2 1 0 5 9
cout<<"\nINCREMENTED NUMBER : "<<comp;
return 0;
}
19 | 0 9 0 2 C S 2 2 1 0 5 9
EXPERIMENT:9
Objective: Design a PDA to accept WCWR where w is any string and WR is
reverse of that string and C is a Special symbol.
Graphical Representation:
Program:
#include <iostream>
#include <stack>
#include <string>
bool isValidWCWR(const std::string& input, char specialSymbol) {
std::stack<char> st;
int i = 0;
int n = input.length();
// Push characters onto the stack until the special symbol is encountered
while (i < n && input[i] != specialSymbol) {
st.push(input[i]);
i++;
// If the special symbol is not found, return false
if (i == n) return false;
// Skip the special symbol
i++;
20 | 0 9 0 2 C S 2 2 1 0 5 9
// Pop characters from the stack and compare with the remaining input
while (i < n) {
if (st.empty() || st.top() != input[i]) {
return false;
st.pop();
i++;
// If the stack is empty, the input is valid
return st.empty();
int main() {
std::string input;
char specialSymbol;
// Input the string and the special symbol
std::cout << "Enter the string: ";
std::cin >> input;
std::cout << "Enter the special symbol: ";
std::cin >> specialSymbol;
// Check if the input is valid
if (isValidWCWR(input, specialSymbol)) {
std::cout << "The string is valid for the language WCW^R." << std::endl;
} else {
std::cout << "The string is not valid for the language WCW^R." << std::endl;
return 0;
[]
21 | 0 9 0 2 C S 2 2 1 0 5 9
EXPERIMENT:10
Objective: Design a Turing machine that’s accepts the following language anbncn where
n>0.
Graphical Representation:
Program:
#include <iostream>
#include <string>
using namespace std;
bool isAnBnCn(const string& input) {
string tape = input;
int n = tape.size();
// Check basic conditions for the string
if (n == 0 || n % 3 != 0) return false;
while (!tape.empty()) {
size_t aIndex = tape.find('a');
size_t bIndex = tape.find('b');
size_t cIndex = tape.find('c');
// Ensure the order is correct
if (aIndex == string::npos || bIndex == string::npos || cIndex == string::npos) return false;
if (aIndex > bIndex || bIndex > cIndex) return false;
22 | 0 9 0 2 C S 2 2 1 0 5 9
// Remove one occurrence of 'a', 'b', and 'c'
tape.erase(aIndex, 1);
tape.erase(bIndex - 1, 1); // Adjust index since one char was removed
tape.erase(cIndex - 2, 1); // Adjust index since two chars were removed
return true;
int main() {
string input;
cout << "Enter a string (a^n b^n c^n): ";
cin >> input;
if (isAnBnCn(input)) {
cout << "The string is accepted." << endl;
} else {
cout << "The string is rejected." << endl;
return 0;
23 | 0 9 0 2 C S 2 2 1 0 5 9
File Submitted by: Shweta Shukla (0902CS221059.)
Session: Jul-Dec 2024 1