Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Main {
- public static void main(String[] args) {
- do {
- String word1, word2, sub; // Initialize 3 string varialbes
- Scanner scanner = new Scanner(System.in); // Initialize Scanner
- // Get the user input as value word1
- System.out.print("Enter the first word: ");
- word1 = scanner.nextLine();
- sub = word1.substring(word1.length()-2); // get the last 2 letters
- // Get the user input as value word2
- System.out.print("Enter the second word: ");
- word2 = scanner.nextLine();
- // Initialize pattern and matcher
- Pattern pattern = Pattern.compile("[a-zA-Z]{1,2}" + sub); // Allows small and capital letter {1,2} means 1 or 2 characters
- Matcher matcher = pattern.matcher(word2);
- // Check if matcher is true
- if(matcher.matches()) {
- System.out.println(word2 + " rhymes " + word1);
- }
- else {
- System.out.println("I'm not sure! Sorry!");
- System.exit(0); // Stop the program
- }
- System.out.println("\n");
- } while(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement