I was trying to reverse String using recursion as follows.
But when I am calling reverse(arr,start++,end--)
it is giving stackoverflow error.
I tried using reverse(arr,start+1,end-1)
then it is working fine.
public class Reverse {
public static void main(String[] args) {
char inp[] = "TestString".toCharArray();
int n = inp.length;
reverse(inp, 0,n-1);
System.out.println(String.valueOf(inp));
}
public static void reverse(char[] arr, int start, int end){
if(start>=end)
return ;
char tmp;
tmp = arr[start];
arr[start] = arr[end];
arr[end] = tmp;
reverse(arr,start+1,end-1);//error line
}
What is the problem with reverse(arr,start++,end--)? I want to know why value of start++ and end-- will not get passed to function.