My method is supposed to reverse the even indices of a given String.
EDITED WITH UPDATED CODE:
public static String revEven(String inString)
{
String tempString = new String();
if (inString.length() <= 2)
return inString;
if (inString.length() == 3)
{
tempString += inString.charAt(2);
tempString += inString.charAt(1);
tempString += inString.charAt(0);
return tempString;
}
if (inString.length() % 2 == 0)
{
return tempString +=
revEven(inString.substring(0, inString.length() - 1)) +
inString.charAt(inString.length() - 1);
}
else
{
return tempString +=
inString.charAt(inString.length() - 1) +
inString.charAt(1) +
revEven(inString.substring(2, inString.length() - 2)) +
inString.charAt(inString.length() - 2) +
inString.charAt(0);
}
}
I ran through the recursion on paper, and it should be working correctly. When I enter abcde
for a test I get 199cda
printed out. It should print out ebcda
.
It seems like it's printing out memory addresses or something, but I can't seem to figure out why.