Lab TOC CS 501
Lab TOC CS 501
Branch: CS
Semester: V
Lab Manual
Theory of Computation [CS 501]
Faculty In-charge
Prof. Om Singh Parihar
[Department of CSE]
Session: 2020-21
Index
SN AIM DATE SIGN REMARK
1 Design a Program for creating
machine that accepts three
consecutive zero
#include <stdio.h>
int main()
{
char str1[80], str2[80]="000";
int l, i, j;
if (j == l)
{
printf("String Accepted in Language ");
}
else
{
printf("String Rejected not in Language");
}
return 0;
}
INPUT:1001001
String Rejected not in Language
Aim2. Design a Program for creating machine that accepts the string
always ending with 101.
#include <iostream>
#include<string.h>
using namespace std;
int main()
{ char str[50];
int i,j, n;
char endstr[]="101";
cout<<"\nEnter a string over input alphabets {0,1}\n";
cin>>str;
n=strlen(str);
for(i=n-3,j=0;i<n,j<3;i++,j++)
{
if(endstr[j]==str[i] )
{
}
else
{
cout<<"String Rejected...";
exit(0);
}
if(j==3)
{
cout<<"String Accepted...";
exit(0);
}
return 0;
}
OUTPUT:
INPUT:1110101
String Accepted..
INPUT:11101011
String Rejected..
Aim3. Design a DFA which will accept all the strings ending with 00 over an
alphabet {0, 1} and write a program to implement the DFA.
#include <stdio.h>
main()
char str[max],f='a';
int i;
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
switch(f)
break;
break;
break;
break;
else printf("\nString is not accepted as it reached %c state which is not the final
state.",f);
OUTPUT:
INPUT:111100
INPUT:1111001
#include <stdio.h>
main()
char str[max],f='1';
int i;
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
switch(f)
break;
OUTPUT:
INPUT:abab
INPUT:bab
#include <stdio.h>
main()
char str[max],f='1';
int i;
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
switch(f)
break;
break;
else printf("\nString is not accepted as it reached %c state which is not the final
state.",f);
OUTPUT:
INPUT:aab
INPUT:baba
#include <iostream>
using namespace std;
int main() {
int dnum;
cout<<”enter any decimal no:”;
cin>>dnum;
if(dnum % 2 == 0)
cout<<num<<" is Divisible by 2";
else
cout<<num<<" is not Divisible by 2";
return 0;
}
OUTPUT:
enter any decimal no:12
12 is is Divisible by 2
// Driver code
int main()
{
string str = "00000101";
cout << “Two’s Complement :”findTwoscomplement(str);
return 0;
}
OUTPUT:
INPUT:100100
Two’s Complement :011100
Aim8. Design a program for creating a machine which accepts string
having equal no. of 1’s and 0’s.
#include <iostream>
#include<string.h>
using namespace std;
int main()
{ char str[50];
int i, n,CountZero=0,CountOne=0;
;
cout<<"\nEnter a string over input alphabets {0,1}\n";
cin>>str;
n=strlen(str);
for(i=0;i<n;i++)
{
if(str[i]=='0' )
{
CountZero++;
}
else
{
CountOne++;
if(CountOne==CountZero)
cout<<"String Accepted...";
else
cout<<"String Rejected...";
return 0;
}
OUTPUT:
Enter a string over input alphabets {0,1}
101010
String Accepted...
#include <iostream>
#include<string.h>
using namespace std;
int main()
{ char str[50];
int i, n,CountZero=0,CountOne=0;
;
cout<<"\nEnter a string over input alphabets {0,1}\n";
cin>>str;
n=strlen(str);
for(i=0;i<n;i++)
{
if(str[i]=='0' )
{
CountZero++;
}
else
{
CountOne++;
cout<<"NO of 1’s"<<CountOne<<endle;
cout<<"NO of 0’s"<<CountOne<<endle
return 0;
}
OUTPUT:
Enter a string over input alphabets {0,1}
101010
NO of 1’s:3
No of 0’s:3
#include <stdio.h>
int main() {
char str[100],f='a';
int i;
printf("Enter Any string over input alphabets {0,1} : \n ");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
switch(f)
{
case 'a': if(str[i]=='0') { printf("1");f='a'; } else {printf("0");f='a'; }
break;
}
}
return 0;
}
Enter Any string over input alphabets {0,1} :
Input: 1010101
Output: 0101010
Aim11. Design a Program which will increment the given binary number by 1.
#include <stdio.h>
int main()
{
long int binary1, binary2;
int i = 0, remainder = 0, sum[20];
printf("Enter any binary number :");
scanf("%ld", &binary1);
while(binary1!= 0 || binary2 != 0)
{
sum[i++] = (binary1 % 10 + binary2 % 10 + remainder) % 2;
remainder = (binary1 % 10 + binary2 % 10 + remainder) / 2;
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}
if(remainder != 0)
sum[i++] = remainder;
--i;
printf("Sum :");
while(i >= 0)
printf("%d", sum[i--]);
return 0;
}
OUTPUT:
Enter any binary number:110001
110010
Aim12. Design a Program to create PDA machine that accept the well-
formed parenthesis.
switch (expr[i]) {
case ')':
case '}':
case ']':
// Driver code
int main()
{
string expr = "{()}[]";
// Function call
if (areBracketsBalanced(expr))
cout << "Balanced";
else
cout << "Not Balanced";
return 0;
}
OUTPUT: Balanced
Aim13. Design a Push Down automata for L={(0^n1^n| n>=1}
#include <iostream>
#include<string.h>
using namespace std;
st.push(a[i]);
}
else
break;
}
if(i<n/2)
{
cout<<"String Rejected...";
exit(0);
}
for(j=i;j<n;j++)
{
if(a[i]=='1' )
{
st.pop(a[i]);
if(top==-1)
{
cout<<"\nString Accepted.\n";
}
else
{
cout<<"\nString Rejected.\n";
}
return 0;
}
OUTPUT:
INPUT:0011
String Accepted.
INPUT:0101
String Rejected.
R
Aim14. Design a PDA to accept WCW where w is any string and WR is
reverse of that string and C is a Special symbol.
δ(q1, b, b) = (q1, ε)
δ(q1, a, a) = (q1, ε)
δ(q1, ε, Z) = (qf, Z)
PDA state transition diagram
Aim14. Design a Turing machine that’s accepts the following language
anbncn where n>0