❓ Help Code

Status
Not open for further replies.
Solution
🤖
Here is a Java program that determines if two words rhyme:

Java:
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!")...
Here is a Java program that determines if two words rhyme:

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

Explanation:
1. The program uses the Scanner, Pattern, and Matcher classes, so we need to import them using the import statements.
2. We enable user input by creating a Scanner object to read input from the user.
3. We create three String variables: word1 to store the first word, word2 to store the second word, and sub to store the last two letters of word1.
4. We initialize a 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.
5. We initialize a Matcher object to store a possible match between the pattern and word2.
6. We use the 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."

You can run this program and test it by entering two words and checking if they rhyme based on the specified criteria.
 
Solution
Status
Not open for further replies.

Similar threads

About this Thread

  • 1
    Replies
  • 270
    Views
  • 1
    Participants
Last reply from:
Unknown user

Online now

Members online
1,299
Guests online
4,110
Total visitors
5,409

Forum statistics

Threads
2,321,948
Posts
29,213,432
Members
1,177,188
Latest member
SGGmingv1
Back
Top