# 16. Scramble

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

## Code

```java
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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pisaucer.gitbook.io/asu-java/docs/16.scramble.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
