0

I need to create a recrusive function to generate a random string - When setting the recrusive as following - the char value - starting from a is not increased - What am i doing wrong?

private static String findPasswordTest2(Password p, int length, String testString, char a)
{
    if (p.isPassword(testString))
    {
        return testString;
    }

    if (a!='z')
    {
        findPasswordTest2(p, length, testString, a++);
        findPasswordTest2(p, length, testString+a, a++);
    }

    findPasswordTest2(p, length, testString+a, a);

    if (p.isPassword(testString))
    {
        return testString;
    }

    return "error";
}
Teivaz
  • 5,462
  • 4
  • 37
  • 75

1 Answers1

1

Instead of doing post-increment , you should do pre-increment. When using a++ , although increments to the next char , but since it is post increment , it would return the initial value. Doing in the following manner would give you the desired result :

 findPasswordTest2(p,length,testString,++a);
 findPasswordTest2(p,length,testString+a,++a);
Rambler
  • 4,994
  • 2
  • 20
  • 27