user3302642
user3302642

Reputation: 1

To find a proper subset of string through recursion in java

char d;
String e="",i1="",d1="";
public static void main(String args[])throws IOException
{
    String str="";
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the string");
    str=br.readLine();
    int l,i=0;
    l=str.length();
    char k;
    String f="";
    Vivek obj=new Vivek();
    while(i<l)
    {
        k=str.charAt(i);
        f=Character.toString(k);
        System.out.println(k);
        obj.recursive(str,f,i+1,l);
        i++;
    }
}
public void recursive(String s,String c,int i,int l)
{
    for(int j=i;j<l;j++)
    {
        d=s.charAt(j);
        d1=Character.toString(d);
        System.out.println(c+d1);
    }
    //System.out.println();
    i1=Character.toString(s.charAt(i)); 
    e=c+i1;
    //System.out.pritnln(e);
    recursive(s,e,i+1,l);
}

The Output of the program is if string abcd is input

it then print out the error

StringIndexOutOfBounds

...Can u tell me how to make it right

Upvotes: 0

Views: 499

Answers (1)

Smutje
Smutje

Reputation: 18123

You don't have any break condition in your recursive function, so it runs like forever - not fully understanding your code, I would propose something like the following as the first line of recursive: if (i == l) { return; }.

Upvotes: 2

Related Questions