Lab TOC CS 501
Lab TOC CS 501
Branch: CS
Semester: V
Practical File
Theory of Computation [CS 501]
Faculty In-charge
Prof. Om Singh Parihar
[Department of CSE]
Session: 2021-22
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()
{ charstr[50];
inti,j, n;
charendstr[]="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()
charstr[max],f='a';
inti;
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
switch(f)
break;
break;
break;
break;
elseprintf("\nString is not accepted as it reached %c state which is not the final state.",f);
OUTPUT:
INPUT:111100
INPUT:1111001
OUTPUT: String is not accepted as it reached d state which is not the final state
Aim:4Design a DFA for |W| Mode 2=0 Machine over input alphabets {a,b}
#include <stdio.h>
main()
charstr[max],f='1';
inti;
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
switch(f)
break;
break;
}
if(f=='1') printf("\nString is accepted as it reached the final state %c at the end.",f);
elseprintf("\nStringis not accepted as it reached STATE %c which is not the final state.",f);
OUTPUT:
INPUT:abab
INPUT:bab
OUTPUT: String is not accepted as it reached 2 state which is not the final state
Aim5.Design a DFA for |w| Mode 3=0 Machine over input alphabets {a,b}
#include <stdio.h>
main()
charstr[max],f='1';
inti;
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
switch(f)
break;
break;
break;
}
}
elseprintf("\nString is not accepted as it reached %c state which is not the final state.",f);
OUTPUT:
INPUT:aab
INPUT:baba
OUTPUT: String is not accepted as it reached 2 state which is not the final state
OUTPUT:
enter any decimal no:12
12 is is Divisible by 2
// Driver code
intmain()
{
stringstr = "00000101";
cout<<“Two’s Complement :”findTwoscomplement(str);
return0;
}
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()
{ charstr[50];
inti, 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...
Aim9. Design a program for creating a machine which count number of 1’s and 0’s in a
given string.
#include <iostream>
#include<string.h>
using namespace std;
int main()
{ charstr[50];
inti, 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
Aim10.Design a Program for Mealy machine to find 1’s complement of a given binary
number.
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()
{
longint binary1, binary2;
inti = 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()
{
stringexpr = "{()}[]";
// Function call
if (areBracketsBalanced(expr))
cout<< "Balanced";
else
cout<< "Not Balanced";
return 0;
}
OUTPUT: Balanced
#include <iostream>
#include<string.h>
using namespace std;
using namespace std;
int top;
char s[10];
class Stack
{
public:
void push(int x)
{
s[top++]=x;
}
void pop(int x)
{
s[top--]=x;
}
};
int main()
{
inti,j, n;
char a[10];
cout<<"\nProgram For PDA Which Accpets Strings Of (0^n)(1^n)\n";
cout<<"\nEnter String::";
cin>>a;
n=strlen(a);
Stack st;
top=-1;
for(i=0;i<n/2;i++)
{
if(a[i]=='0' )
{
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
n
Aim14.Design a Turing machine that’s accepts the following languagea bncn where n>0