import java.io.*;
class fa
{
public static void main(String args[])
throws IOException
{
String str;
char ch,ch1;
char ipsy[] = new char[5];
int len,temp=0,no,st1,cst,fst=0,temp1=0,isy=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter The number of States:-");
st1 = Integer.parseInt(br.readLine());
System.out.print("Enter The number of final State:-");
fst = Integer.parseInt(br.readLine());
System.out.print("Enter The number of Input Symbols:-");
isy = Integer.parseInt(br.readLine());
//System.out.println(isy);
int z=0;
do
{
System.out.println("Enter the input Symbol " + (z+1) +" : ");
ch1 = (char) br.read();
ch = (char) br.read();
ch = (char) br.read();
z++;
}while(z!=(isy));
System.out.print("Enter a String:- ");
str=br.readLine();
len= str.length();
int st[][] = new int [st1][2];
for(int x = 0 ; x < st1 ; x++)
{
for(int y = 0 ; y < isy ; y++)
{
System.out.print("Enter the state which we reach when going through " + ipsy[y] + " from state " + (x+1) + ":-");
temp = Integer.parseInt(br.readLine());
st[x][y]=temp;
}
}
cst=1;
for(int x = 0 ; x < len ; x++)
{
temp=cst;
ch = str.charAt(x);
for(int y = 0 ; y < isy ; y++)
{
if (ch==ipsy[y])
{
if (st[cst-1][y]!=0)
{
cst=st[cst-1][y];
}
}
}
System.out.println("Moving from state S" + temp + " to S" + cst );
}
if (cst==fst)
{
System.out.println("The String is acceptable");
}
else
{
System.out.println("The String is not acceptable");
}
}
}1
Showing posts with label Java Programs. Show all posts
Showing posts with label Java Programs. Show all posts
Tuesday, March 31, 2009
Tuesday, March 24, 2009
Generalized FA program for two input symbols
//You have to enter the state table for this program.....
import java.io.*;
class genfa
{
public static void main(String args[])
throws IOException
{
String str;
char ch;
int len,temp=0,no,st1,cst,fst=0,temp1=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a String:- ");
str=br.readLine();
len= str.length();
System.out.print("Enter The number of States:-");
st1 = Integer.parseInt(br.readLine());
System.out.print("Enter The number of final State:-");
fst = Integer.parseInt(br.readLine());
//System.out.print("Enter The number of Input Symbols:-");
//st = Integer.parseInt(br.readLine());
int st[][] = new int [st1][2];
for(int x=0 ; x < st1 ; x++)
{
System.out.print("Enter the state which we reach when going through 'a' from state " + (x+1) + ":-");
temp = Integer.parseInt(br.readLine());
st[x][0]=temp;
System.out.print("Enter the state which we reach when going through 'b' from state " + (x+1) + ":-");
temp = Integer.parseInt(br.readLine());
st[x][1]=temp;
}
cst=1;
for(int x=0 ; x < len ; x++)
{
temp=cst;
ch = str.charAt(x);
if (ch=='a')
{
if (st[cst-1][0]!=0)
{
cst=st[cst-1][0];
}
}
else if(ch=='b')
{
if (st[cst-1][1]!=0)
{
cst=st[cst-1][1];
}
}
System.out.println("Moving from state S" + temp + " to S" + cst );
}
if (cst==fst)
{
System.out.println("The String is acceptable");
}
else
{
System.out.println("The String is not acceptable");
}
}
}
import java.io.*;
class genfa
{
public static void main(String args[])
throws IOException
{
String str;
char ch;
int len,temp=0,no,st1,cst,fst=0,temp1=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a String:- ");
str=br.readLine();
len= str.length();
System.out.print("Enter The number of States:-");
st1 = Integer.parseInt(br.readLine());
System.out.print("Enter The number of final State:-");
fst = Integer.parseInt(br.readLine());
//System.out.print("Enter The number of Input Symbols:-");
//st = Integer.parseInt(br.readLine());
int st[][] = new int [st1][2];
for(int x=0 ; x < st1 ; x++)
{
System.out.print("Enter the state which we reach when going through 'a' from state " + (x+1) + ":-");
temp = Integer.parseInt(br.readLine());
st[x][0]=temp;
System.out.print("Enter the state which we reach when going through 'b' from state " + (x+1) + ":-");
temp = Integer.parseInt(br.readLine());
st[x][1]=temp;
}
cst=1;
for(int x=0 ; x < len ; x++)
{
temp=cst;
ch = str.charAt(x);
if (ch=='a')
{
if (st[cst-1][0]!=0)
{
cst=st[cst-1][0];
}
}
else if(ch=='b')
{
if (st[cst-1][1]!=0)
{
cst=st[cst-1][1];
}
}
System.out.println("Moving from state S" + temp + " to S" + cst );
}
if (cst==fst)
{
System.out.println("The String is acceptable");
}
else
{
System.out.println("The String is not acceptable");
}
}
}
Dfa for all languages ending with aa OR (a+b)*aa
//works for strings which contains a and b as input symbols and
//the string should end with "aa"
import java.io.*;
class aadfa
{
public static void main(String args[])
throws IOException
{
String str;
char ch;
int len,st=1,temp;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a String:- ");
str=br.readLine();
len= str.length();
for(int x=0 ; x < len ; x++)
{
ch = str.charAt(x);
temp = st;
switch(st)
{
case 1:
if (ch=='a')
{
st=2;
}
else
{
st=1;
}
break;
case 2:
if (ch=='a')
{
st=3;
}
else
{
st=1;
}
break;
case 3:
if (ch=='a')
{
st=3;
}
else
{
st=1;
}
break;
}
System.out.println("Moving from state S" + temp + " to S" + st );
}
if (st==3)
{
System.out.println("The String is acceptable");
}
else
{
System.out.println("The String is not acceptable");
}
}
}
//the string should end with "aa"
import java.io.*;
class aadfa
{
public static void main(String args[])
throws IOException
{
String str;
char ch;
int len,st=1,temp;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a String:- ");
str=br.readLine();
len= str.length();
for(int x=0 ; x < len ; x++)
{
ch = str.charAt(x);
temp = st;
switch(st)
{
case 1:
if (ch=='a')
{
st=2;
}
else
{
st=1;
}
break;
case 2:
if (ch=='a')
{
st=3;
}
else
{
st=1;
}
break;
case 3:
if (ch=='a')
{
st=3;
}
else
{
st=1;
}
break;
}
System.out.println("Moving from state S" + temp + " to S" + st );
}
if (st==3)
{
System.out.println("The String is acceptable");
}
else
{
System.out.println("The String is not acceptable");
}
}
}
For each
class foreach
{
public static void main(string args[])
{
int nums[]={1,2,3,4,5,6,7,8,9,10};
for(int x : nums)
{
system.out.print(x + " ");
x=x*10;
}
}
}
{
public static void main(string args[])
{
int nums[]={1,2,3,4,5,6,7,8,9,10};
for(int x : nums)
{
system.out.print(x + " ");
x=x*10;
}
}
}
Subscribe to:
Posts (Atom)