Reverse A String
Code:
Using recursion and substring:
private static String reverse(String str)
{
// base case: if the string is null or empty
if (str == null || str.equals("")) {
return str;
}
// last character + recur for the remaining string
return str.charAt(str.length() - 1) +
reverse(str.substring(0, str.length() - 1));
}
Reverse A Number
Using collection :
static void reverse(Integer myArray[])
{
Collections.reverse(Arrays.asList(myArray));
System.out.println("Reversed Array:" + Arrays.asList(myArray));
}