Basically we were assigned to write a method using the following heading that wh
ID: 3634299 • Letter: B
Question
Basically we were assigned to write a method using the following heading that when given two strings, it returns true if the second string is the first sting in reverse order. Capitalization does matter whether it will return true or false and only recursion techniques can be used to construct this method. Also because this method returns a boolean output, their should be no print or println statements.public boolean sameRev(String str1, String str2){
}
Here are some examples of what a few test might return when implemented using this method:
sameRev("abc", "cba") = true
sameRev("abc", "abc") = false
sameRev("Abc", "cba") = false
Thanks for the help, and remember this must be created using a single method....
Explanation / Answer
public boolean sameRev(String str1, String str2)
{
// first check lengths
if(str1.length() != str2.length())
return false;
// break case: no characters
if(str1.length() == 0)
return true;
// check 1st character
if(str1.charAt(0) != str2.charAt(str2.length()-1))
return false;
// check rest of string
return sameRev(str1.substring(1), str2.substring(0, str2.length()-1));
}