String
and returns the reversed version of the String
.
Examples:
reverseString("abcde") -> "edcba"
reverseString("1") -> "1"
reverseString("") -> ""
reverse("madam") -> "madam"
reverse(null) -> null
StringBuilder
class to build the reversed String
.
charAt(i)
method of the String
class and append()
of the StringBuilder
class useful. Remember - Strings in java are immutable. Each time a String
is changed, a new copy of the String
is made. For any changes to a String
, using a StringBuilder
object results in lower memory consumption and faster code.
String
from str.length()-1
to 0
, adding the character at index i (using .charAt(i)
) to the new StringBuilder
object with .append(i)
..toString()
method of the StringBuilder
object created earlier.
public static String reverseString(String str){ String inputString = str; String outputString = null; return outputString; }
C
Java
Python