Source of WordScrambler.java


  1: //WordScrambler.java

  3: import java.util.Scanner;

  5: public class WordScrambler
  6: {
  7:     /* Output every possible combination of a word.
  8:        Each recursive call moves a letter from
  9:        remainLetters" to scramLetters".
 10:     */
 11:     public static void scrambleLetters
 12:     (
 13:         String remainLetters, // Remaining letters
 14:         String scramLetters   // Scrambled letters
 15:     )
 16:     {
 17:         String tmpString; // Temp word combinations
 18:         int i;            // Loop index

 20:         if (remainLetters.length() == 0) // Base case: All letters used
 21:         {
 22:             System.out.println(scramLetters);
 23:         }
 24:         else // Recursive case: move a letter from
 25:         {
 26:             // remaining to scrambled letters
 27:             for (i = 0; i < remainLetters.length(); ++i)
 28:             {
 29:                 // Move letter to scrambled letters
 30:                 tmpString = remainLetters.substring(i, i + 1);
 31:                 remainLetters = removeFromIndex(remainLetters, i);
 32:                 scramLetters = scramLetters + tmpString;

 34:                 scrambleLetters(remainLetters, scramLetters);

 36:                 // Put letter back in remaining letters
 37:                 remainLetters = insertAtIndex(remainLetters, tmpString, i);
 38:                 scramLetters =
 39:                     removeFromIndex(scramLetters, scramLetters.length() - 1);
 40:             }
 41:         }
 42:     }

 44:     // Returns a new String without the character at location remLoc
 45:     public static String removeFromIndex
 46:     (
 47:         String origStr,
 48:         int remLoc
 49:     )
 50:     {
 51:         String finalStr; // Temp string to extract char

 53:         finalStr = origStr.substring(0, remLoc); // Copy before location remLoc
 54:         // Copy after location remLoc
 55:         finalStr += origStr.substring(remLoc + 1, origStr.length());

 57:         return finalStr;
 58:     }

 60:     // Returns a new String with the character specified by insertStr
 61:     // inserted at location addLoc
 62:     public static String insertAtIndex
 63:     (
 64:         String origStr,
 65:         String insertStr,
 66:         int addLoc
 67:     )
 68:     {
 69:         String finalStr; // Temp string to extract char

 71:         finalStr = origStr.substring(0, addLoc); // Copy before location addLoc
 72:         finalStr += insertStr; // Copy character to location addLoc

 74:         finalStr += origStr.substring(addLoc, origStr.length());

 76:         return finalStr;
 77:     }

 79:     public static void main(String[] args)
 80:     {
 81:         Scanner scnr = new Scanner(System.in);
 82:         String wordScramble; // User defined word to scramble

 84:         // Prompt user for input
 85:         System.out.print("Enter a word to be scrambled: ");
 86:         wordScramble = scnr.next();

 88:         // Call recursive method
 89:         scrambleLetters(wordScramble, "");
 90:     }
 91: }