Suppose there is a string variable str = "A man is no one!"
, then i need the reverse of this string to be stored in same str variable as "!eno on si nam A"
, without using any other object or variable. Could someone please provide the most optimized way to do this?
I already used below piece of code:
public static String reverseRecursively(String str) {
//base case to handle one char string and empty string
if (str.length() < 2) {
return str;
}
return reverseRecursively(str.substring(1)) + str.charAt(0);
}
Any other way of doing this?
Please note that I only want it using String class methods
.