-1
void recur(int i)
{
    if(i==n)
        return;
    String sub="";
    for(int j=i+1;j<n;j++)
    {
        sub=s.substring(i,j);
        if(isPalindrome(sub))
            System.out.println(sub);
    }
    recur(i++);
}

I am encountering a StackOverflowError at the

sub=s.substring(I,j); 

statement.

s="geeks", initial value of I=0;
Bart Friederichs
  • 32,037
  • 14
  • 96
  • 185

2 Answers2

1
recur(i++);

The value of the expression i++ is the value of i at the current time; and afterwards you increment it.

As such, you are basically invoking:

recur(i);
i++;

And so you are just invoking recur again with the same parameter.

Try:

recur(++i);
Andy Turner
  • 131,952
  • 11
  • 151
  • 228
1

Try This

public class P {

    public static final String s="geeks";

    static void recur(int i){

        int n=6;   //Size of string

        if(i==n)
            return;

        String sub="";

        for(int j=i+1;j<n;j++)
        {
            sub=s.substring(i,j);
            //Any Function
            System.out.println(sub);
        }
        recur(++i);
    }


    public static void main(String[] args) {

        P.recur(0);
    }
}
Prabath
  • 71
  • 4