import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Random; public class WordMuddler { public static List breakers = new ArrayList(); public static List words = new ArrayList(); public static boolean startedOnWords; /** * @param args */ public static void main(String[] args) { Random random = new Random(); String sentence = "There is not a shred of evidence that the Duke of Edinburgh ordered Princess Diana's death or that it was organised by MI6, the inquest has heard. Coroner Lord Justice Scott Baker said jurors could find that Diana and Dodi Al Fayed died accidentally, or because of negligence."; int last = 0; startedOnWords = Character.isLetter(sentence.charAt(0)); boolean onWords = startedOnWords; int i; for(i = 0; i < sentence.length(); i++) { if(Character.isLetter(sentence.charAt(i))) { if(!onWords) { breakers.add(sentence.substring(last, i)); onWords = true; last = i; } } else { if(onWords) { words.add(sentence.substring(last, i)); onWords = false; last = i; } } } if(onWords) { words.add(sentence.substring(last, i)); onWords = false; } else { breakers.add(sentence.substring(last, i)); onWords = true; } System.out.println("Original:"); print(); List jumbledWords = new ArrayList(); for(String word : words) { if(word.length() > 3) { StringBuilder newWord = new StringBuilder(word.length()); newWord.append(word.charAt(0)); List midChars = new ArrayList(); for(i = 1; i < word.length() - 1; i++) { midChars.add(word.charAt(i)); } while(midChars.size() > 0) { newWord.append(midChars.remove(random.nextInt(midChars.size()))); } newWord.append(word.charAt(word.length() - 1)); jumbledWords.add(newWord.toString()); } else { jumbledWords.add(word); } } words = jumbledWords; System.out.println(); System.out.println("Jumbled:"); print(); } public static void print() { Iterator iWords = words.iterator(); Iterator iBreakers = breakers.iterator(); if(!startedOnWords && iBreakers.hasNext()) { System.out.print(iBreakers.next()); } while(iWords.hasNext() && iBreakers.hasNext()) { if(iWords.hasNext()) { System.out.print(iWords.next()); } if(iBreakers.hasNext()) { System.out.print(iBreakers.next()); } } System.out.println(); } }