/**
*
* @author Lawrence
*/
import java.util.*;
public class scramble {
public static void main(String[] args) {
int randomPos, start=0;
char first, last;
/* String text = "This is a pretty simple paragraph. The season is autumn" +
"and the air is crisp and clear. The leaves are turning a "+
"variety of colors and then sadly drop to the ground";
*/
String text = "What person willing interesting";
Random generator = new Random();
for (int i=0; i<text.length(); i++){
int whiteSpace = text.indexOf(" ", start);
if (whiteSpace<0){break;} else{
first = text.charAt(start);
last = text.charAt(whiteSpace-1);
System.out.print(first);
// this is the first character for each word. need the last and scramble the middle
String eachWord = text.substring(start+1 , whiteSpace-1) ;
//this will give me the substring to scramble, need a integer to randomize?
int b = (whiteSpace-1)-(start+1);
// this gives how many numbers to scramble
for (int n=0; n < b; n++){
randomPos = generator.nextInt(b); //outputs a random beween 0&b)
char a = eachWord.charAt(randomPos); //takes the character at pos
System.out.print(a);
// onces i choose that racndom character i need to take it out of the list.
if((start+1)-randomPos <=1){
eachWord = text.substring(randomPos , whiteSpace-1);}
else if((whiteSpace-1)-randomPos <=1){
eachWord = text.substring(start+1 , randomPos); }
else {
eachWord = text.substring(start+1 , randomPos)+ text.substring(randomPos , whiteSpace-1);}
}
System.out.println(last+ " "+ eachWord) ;//this is the last charater of each word!
start = whiteSpace+1;
}
}
}
} |