scramble()
Platform: Java
Published Apr 08, 2009
Updated Apr 08, 2009
This is a method for Java that scrambles up a string. You just add it to your class and call it by doing
scramble(string).
Also, make sure at the top of the project you
import java.util.Random;
Examples:
* variable = scramble(variable);
* System.out.println(scramble("elephant"));
public static String scramble(String word) {
String newword = "";
int rndnum;
Random randGen = new Random();
boolean letter[] = new boolean[word.length()];
do {
rndnum = randGen.nextInt(word.length());
if (letter[rndnum] == false) {
newword = newword + word.charAt(rndnum);
letter[rndnum] = true;
}
} while (newword.length() < word.length());
return newword;
}