import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RhymeChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first word: ");
String word1 = scanner.nextLine();
System.out.print("Enter the second word: ");
String word2 = scanner.nextLine();
String sub = word1.substring(word1.length() - 2);
Pattern pattern = Pattern.compile("[A-Za-z]{1,2}" + sub + " ");
Matcher matcher = pattern.matcher(word2);
if (matcher.matches()) {
System.out.println("The words rhyme!")...
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RhymeChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first word: ");
String word1 = scanner.nextLine();
System.out.print("Enter the second word: ");
String word2 = scanner.nextLine();
String sub = word1.substring(word1.length() - 2);
Pattern pattern = Pattern.compile("[A-Za-z]{1,2}" + sub + " ");
Matcher matcher = pattern.matcher(word2);
if (matcher.matches()) {
System.out.println("The words rhyme!");
} else {
System.out.println("The words do not rhyme.");
}
}
}
Scanner, Pattern, and Matcher classes, so we need to import them using the import statements.Scanner object to read input from the user.String variables: word1 to store the first word, word2 to store the second word, and sub to store the last two letters of word1.Pattern object with a regular E×ρréššion that matches any uppercase or lowercase letters (1 or 2) before the last two letters of word1.Matcher object to store a possible match between the pattern and word2.matches() method of the Matcher object to check if the two words rhyme. If they do, we print "The words rhyme!"; otherwise, we print "The words do not rhyme."