Your Ad Here

Tuesday, March 24, 2009

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");
}

}
}

No comments: