TOC File
TOC File
Submitted by
Shweta Shukla(0902CS221059)
B.Tech. Computer Science & Engineering 5th Semester
(2022-2026 batch)
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.
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.
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.
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";
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 M =s1.length();
int N=s2.length();
int res;
/* A loop to slide pat[] one by one */
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";
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";
}
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";
}
}
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)
{
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>
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;
len=n.length();
comp.resize(len);
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>
std::stack<char> st;
int i = 0;
int n = input.length();
// Push characters onto the stack until the special symbol is encountered
st.push(input[i]);
i++;
if (i == n) return false;
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) {
return false;
st.pop();
i++;
return st.empty();
int main() {
std::string input;
char specialSymbol;
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>
int n = tape.size();
if (n == 0 || n % 3 != 0) return false;
while (!tape.empty()) {
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);
return true;
int main() {
string input;
if (isAnBnCn(input)) {
} else {
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