Write a Java program (Project6.java) that prints all strings defined by a regula
ID: 3799775 • Letter: W
Question
Write a Java program (Project6.java) that prints all strings defined by a regular expression up to a given length. Assume the alphabet = {0,1}. Prompt the user to enter a regular expression and an integer n. Print all the strings, with length n , defined by the regular expression. Use console based I/O. [The empty string can be printed with the Unicode “u03B5”, but Unicode may not be supported in some environments.]
A sample run of the program is shown bellow.
Regular expression: 01*|(10)*
n: 5
------- Strings in the language -------
0
01
10
011
0111
1010
01111
This program will prompt a user to enter a regular expression and an integer n. Please write program in Java from scratch.
Explanation / Answer
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class A {
public static void main(String args[]) {
Pattern p = Pattern.compile("o1");
Matcher mat = p.matcher("u03B51");
while (mat.find()) {
System.out.println("");
for (int i = 0; i <=10;i++){
System.out.println(" " + mat.group());
}
System.out.println("");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.