Web Analytics Made Easy -
StatCounter recursion problem - CodingForum

Announcement

Collapse
No announcement yet.

recursion problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • recursion problem

    I am tasked with writing a recursion method that will take in a string (word) and then print out every combination of the words letters. I have a start, and a hint from our professor that talks about using the substring method of string.

    public static void printVariations(String chosen, String available)
    {
    String newChosen;
    String newAvailable;
    if (available.length() == 1)
    System.out.println(available + chosen);

    else
    {
    for (int i = 0; i < available.length(); i++)
    {
    newChosen = (chosen + chosen.substring(i));
    newAvailable = (available - available.substring(i));
    printVariation (newChosen, newAvailable);
    }
    }



    }
    obviously the available.length == 1 prints fine, the pseudocode for the remaining is as follows:

    for each character in available
    newChosen = chosen + that character
    newAvailable = available - that character

    then the recursion back to printVariations

    printVariations(newChosen, newAvailable)

    somehow I am supposed to use this substring to make this happen.

    any ideas???

  • #2
    First of all, you are posting in the wrong forum.

    This is the javaSCRIPT forum. NOT the JAVA forum. About the only thing the two languages have in common is the first four letters of their names.

    Secondly, your professor's hint was *ONLY* a hint. You can *NOT* remove a character from a string using the subtraction operator. HINT: look at substring( ) some more.
    Be yourself. No one else is as qualified.

    Comment

    Working...
    X