Regular expression check: a+b(a+b)*

package re_check_3;
import java.util.Scanner;
lic class Re_Check_3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("The R.E is a+b(a+b)*");
System.out.println("Enter the string");
String str = sc.nextLine();
System.out.println("The condition for R.E is to have either a or start with b ");
int length = str.length();
if (length == 1) {
if (str.charAt(0) == 'a' || str.charAt(0) == 'b') {
System.out.println("RE ACCEPTED");
} else {
System.out.println("RE REJECTED");
}
} else {
for (int i = 1; i < length; i++) {
if ((str.charAt(0) == 'b') && (str.charAt(i) == 'a' || str.charAt(i) == 'b')) {
System.out.println("RE ACCEPTED");
break;
} else {
System.out.println("RE REJECTED");
break;
}
}
}
}
}

                    OUTPUT

The R.E is a+b(a+b)*
Enter the string
baab
The condition for R.E is to have either a or start with b
RE ACCEPTED

No comments:

Post a Comment