I am working on recursion.
I need to create a reverseString method in which if i set the
parameter like ReverseString(Maruf, 0) then the output will be
furaM. But if i enter ReverseString(Maruf, 2)
then the output will be raM. It is removing 2 characters from the
reverse.
I have to take 2 parameters.
method signature is this
public static String reverseString(String str, int i){
// Complete the method
}
Example: Starting word is Hello, reversed from index 2 is leH.
public class ReverseStringTest { public static String reverseString(String str, int i) { if (i == 0) return reverseString(str, str.length() - 1); if (i == 1) { return str.charAt(1) + "" + str.charAt(0); } else { return str.charAt(i) + reverseString(str, i - 1); } } public static void main(String[] args) { System.out.println(reverseString("Hello", 2)); System.out.println(reverseString("Maruf", 2)); System.out.println(reverseString("Maruf", 0)); } }
Get Answers For Free
Most questions answered within 1 hours.