Skip to content

Instantly share code, notes, and snippets.

@hakxcore
Created May 9, 2022 02:12
Show Gist options
  • Save hakxcore/71c358354f1fe29b42e7f5528b8c8b8f to your computer and use it in GitHub Desktop.
Save hakxcore/71c358354f1fe29b42e7f5528b8c8b8f to your computer and use it in GitHub Desktop.
## Checks for aa+b*
## Code for String_rec.java
import java.util.*;
//aa+b*
public class String_rec {
String str;
int state=0;
void StrCheck(){
if(str.charAt(0) == 'a'){
state=1;
if((str.length()==2) && (str.charAt(1) == 'a')){
state=2;
}
else{
state=-1;
}
}
else if(str.charAt(0) == 'b'){
state=3;
for(int i=1;i<str.length();i++){
if(str.charAt(i) != 'b'){
state=-1;
}
}
}
if((state == 2) || (state == 3)){
System.out.println("string is accepted");
}
else{
System.out.println("string is rejected");
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String_rec obj=new String_rec();
obj.str=sc.next();
obj.StrCheck();
}
}
## Output
$ java String_rec
aa
string is accepted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment