43.1k views
2 votes
2. Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise.

User Amir Molaa
by
4.7k points

1 Answer

5 votes

Answer:

Step-by-step explanation:

public static boolean isReverse(String a, String b) {

if (a.length() == 0 && b.length() == 0) {

return true;

} else if (a.length() == b.length()) {

int length = b.length();

char letter1 = Character.toUpperCase(a.charAt(0));

char letter2 = Character.toUpperCase(b.charAt(length - 1));

if (letter1 == letter2) {

return isReverse(a.substring(1), b.substring(0, length - 1));

} else {

return false;

}

}

return false;

}

User Kkh
by
5.8k points