16. Scramble

Scramble the words in a paragraph by shuffling the internal letters of each word, keeping the first and last letters in place.

Description: Scramble the words in a paragraph by shuffling the internal letters of each word, keeping the first and last letters in place.

Code

package asu.scramble;

import java.util.Random;
import java.util.Scanner;

public class Scramble {
    
    public static void main(String[] args) {
        String sentence = "According to research at Cambridge University, it doesn't matter \nin what order the letters in a word are, \nthe only important thing is that the first and last \nletters be at the right place. The Rest can\nbe a total mess and you can still read it \nwithout problem. This is because the human mind does \nnot read every letter by itself, but the word\nas a whole";
        
        String out = "";
        Scanner scanner = new Scanner(sentence);
        
        int count = 0;
        while (scanner.hasNext()) {
            String word = scanner.next();
            count++;
            out += innerScramble(word) + " ";
            if ((count % 10) == 0) out += "\n";
        }
        System.out.println(out);
        Scanner consoleInput = new Scanner(System.in);
        System.out.println("");
        System.out.println("Please hit <enter> to see the correct version:");
        consoleInput.nextLine();
        System.out.println(sentence);
    }
    
    //getScramble scrambles the entire input string
    public static String getScramble(String input) {
        String output = input;
        char[] outChars = output.toCharArray();
        Random r = new Random();
        for (int i = 0; i < outChars.length * outChars.length; i++) {
            int swap1 = r.nextInt(outChars.length);
            int swap2 = r.nextInt(outChars.length);
            char temp = outChars[swap1];
            outChars[swap1] = outChars[swap2];
            outChars[swap2] = temp;
        }
        output = String.valueOf(outChars);
        return output;
    }
    
    //innerScramble leaves the first and last letter in place.
    public static String innerScramble(String input) {
        String output = input;
        char[] outChars = output.toCharArray();
        if (outChars.length > 3) {
            Random r = new Random();
            for (int i = 0; i < outChars.length * outChars.length; i++) {
                int swap1 = r.nextInt(outChars.length - 2) + 1;
                int swap2 = r.nextInt(outChars.length - 2) + 1;
                char temp = outChars[swap1];
                outChars[swap1] = outChars[swap2];
                outChars[swap2] = temp;
            }
        }
        output = String.valueOf(outChars);
        return output;
    }
}   

Output

Acdrniocg to resraceh at Cdrgbmiae Utyievrins, it dons'et mtaetr in 
what odrer the ltteres in a word are, the only 
iptmnaort tnihg is that the fsrit and last ltteres be 
at the rgiht paecl. The Rset can be a taotl 
mess and you can slitl read it wihtout pmberol. This 
is bauscee the huamn mind deos not read every leettr 
by itself, but the wrod as a whloe 

Please hit <enter> to see the correct version:

According to research at Cambridge University, it doesn't matter 
in what order the letters in a word are, 
the only important thing is that the first and last 
letters be at the right place. The Rest can
be a total mess and you can still read it 
without problem. This is because the human mind does 
not read every letter by itself, but the word
as a whole

Last updated