Turing Machine Program (Equal Number of 0's and 1's) > Java Program
Theoretical Computer Science
Turing Machine Program (Equal Number of 0's and 1's) > Java Program
import java.io.*;
import java.lang.*;
class turing_machine {
public static void main(String args[])throws IOException {
int i,n1,n,j,z,count=0,count1=0,turing_machine1;
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter Input to turing machine");
String s;
s=in.readLine();
turing_machine1=s.length();
char a[]=s.toCharArray();
System.out.print("String->");
for(i=0;i<turing_machine1;i++) {
System.out.print("\t"+a[i]);
}
System.out.println();
for(i=0;i<turing_machine1;i++) {
if(a[i]=='0') {
a[i]='X'; i=(n/2);
}
if(a[i]=='1') {
a[i]='Y'; i=0; System.out.print("String->");
for(z=0;z<turing_machine1;z++) {
System.out.print("\t"+a[z]);
}
System.out.println();
}
}
for(i=0;i<n;i++) {
if(a[i]=='X') {
count++;
}
if(a[i]=='Y') {
count1++;
}
}
if(count==count1)
System.out.print("String accepted");
else System.out.print("String not accepted");
}
}
/* OUTPUT of turing machine:
Enter input to turing machine
000111
String-> 0 0 0 1 1 1
String-> X 0 0 Y 1 1
String-> X X 0 Y Y 1
String-> X X X Y Y Y
String accepted */
Comments
Post a Comment