TOC File
TOC File
Practical File
on
THEORY OF COMPUTATION(150503)
July-December, 2019
Design a Program for creating machine that accepts three consecutive 1’s.
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cout<<"Enter a string in binary form: ";
cin>>s;
map<int,char> state;
state[0]='A';state[1]='B';state[2]='C';state[3]='D';
int i=0;
for(int c=0;c<s.length();c++)
{
switch(s[c])
{
case '0':
if(i%2)
i=2;
else
i=0;
break;
case '1':
if(i%2==0)
i+=1;
else if(i==3)
i=1;
}
}
cout<<"Current state is "<<state[i];
if(i==3)
cout<<" that means string is accepted. \n";
else
cout<<" that means string is not accepted. \n";
return 0;
}
OUTPUT:
PROGRAM 2
Design a Program for creating machine that accepts the string always
ending with 101.
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cout<<"Enter a string in binary form:";
cin>>s;
map<int,char>state;
state[0]='A';state[1]='B';state[2]='C';state[3]='D';
int i=0;
for(int c=0;c<s.length();c++)
{
if(s[c]=='0')
i=0;
else
{
i+=1;
i%=4;
}
if(i==3)
break;
}
cout<<"Current state is "<<state[i];
}
OUTPUT:
PROGRAM 3
#include<iostream>
using namespace std;
int main()
{
string str;
char state='A';
cout<<"Enter your string: ";
cin>>str;
for(int i=0;i<str.length();i++)
{
switch(str[i])
{
case '0':
state='A';
break;
case '1':
state='B';
}
}
if(state=='A')
cout<<"Machine is in final state \n";
else
cout<<"Machine is not in final state \n";
}
OUTPUT:
PROGRAM 5
#include<iostream>
using namespace std;
int main()
{
int count0=0,count1=0;
string str;
cout<<"Enter your string: ";
cin>>str;
for(int i=0;i<str.length();i++)
{
if(str[i]=='0')
count1++;
if(str[i]=='0')
count0++;
}
if(count0==count1)
cout<<"String is accepted \n";
else
cout<<"String is not accepted \n";
}
OUTPUT:
PROGRAM 6
Design a Program for creating a machine which count number of 1’s and
0’s in a given string.
#include<iostream>
using namespace std;
int main()
{
int count0=0,count1=0;
string str;
cout<<"Enter your string: ";
cin>>str;
for(int i=0;i<str.lengt h();i++)
{
if(str[i]=='1')
count1++;
if(str[i]=='0')
count0++;
}
cout<<"Number of 1's: "<<count1;
cout<<"\nNumber of 0's: "<<count0;
}
OUTPUT:
PROGRAM 7
#include<bits/stdc++.h>
using namespace std;
int main()
{
stack<char> stake;
cout<<"Enter a string: ";
char c;
scanf("%c",&c);
while(c!='\n')
{
stake.push(c);
scanf("%c",&c);
}
if(stake.size()%2==1)
cout<<"String is not an even length string and it is not accepted";
else
cout<<"String is an even length string and it is accepted";
return 0;
}
OUTPUT:
PROGRAM 8
Design a program that accepts the string having second last element 0.
#include<iostream>
using namespace std;
int main()
{
string str;
char state='A';
cout<<"Enter your string:";
cin>>str;
for(int i=0; i<str.length();i++)
{
switch(state)
{
case'A':
if(str[i]=='1')
state='A';
else
state='B';
break;
case 'B':
state='C';
break;
case'C':
if(str[i]=='0')
state='B';
else
state='A';
}
}
if(state=='C')
cout<<"String is accepted";
else
cout<<"String is not accepted";
}
OUTPUT:
PROGRAM 9
Design a Program to create a machine that accepts all the strings having
two consecutive 0’s.
#include<iostream>
using namespace std;
int main()
{
string str;
char state='A';
cout<<"Enter your string:";
cin>>str;
for(int i=0; i<str.length();i++)
{
switch(state)
{
case'A':
if(str[i]=='1')
state='A';
else
state='B';
break;
case 'B':
if(str[i]=='1')
state='A';
else
state='C';
break;
case'C':
state='C';
}
}
if(state=='C')
cout<<"String is accepted";
else
cout<<"String is not accepted";
}
OUTPUT:
PROGRAM 10
#include<bits/stdc++.h>
using namespace std;
int main()
{
stack<char>stake;
stake.push('$');
string ins;
cout<<"Enter a string: ";
cin>>ins;
int i=0,bad=0;
switch(ins[i])
{
case'a':
stake.push('x');
while(ins[++i]=='a')
{
stake.push('x');
}
if(ins[i-1]!='a')
{
bad=1;break;
}
while(ins[++i]=='b')
{
continue;
}
if(ins[i-1]!='b')
{
bad=1;break;
}
while(ins[i++]=='c')
{
stake.pop();
if(stake.size()==1)
break;
}
break;
default:
bad=1;
}
if(bad || i!=ins.length())
cout<<"String is not accepted \n";
else
cout<<"String is accepted \n";
return 0;
}
OUTPUT: