In: Computer Science
java:
Sample output: Aoccdrnig to reacresh at an Elingsh uinervtisy, it deos not mttaer in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht the frist and lsat ltteer are in the rghit pclae. The rset can be a toatl mses and you can sitll raed it wouthit a porbelm. Tihs is bcuseae we do not raed ervey lteter by it slef but the wrod as a wlohe and the biran fguiers it out aynawy.
PROGRAM DESCRIPTION: In this assignment, you will read an unspecified number of lines of text from STDIN. Input is terminated with EOF (ctrl-d for keyboard input). Your only output, to STDOUT, will echo the input except that the letters of any word with more than three characters will be randomly shuffled (except for the first and last letters). Don't scramble any punctuation, and you may assume that punctuation will only occur at the end of a word. Any word will have, at most, one punctuation character. Punctuation does not count towards the length of the word. You may also assume that each word will be separated by one space and that a line will have no leading or trailing whitespace. Blank lines are allowable. You must preserve the newlines (output must have the same number of lines and the same number of words on each line as when input.) (A word, here, is defined as a sequence of alphanumeric characters delimeted by whitespace.) For this assignment you must code the shuffle algorithm as described in class, and not a library shuffle function.
Sample input:According to research at an English university, it does not matter in what order the letters in a word are, the only important thing is that the first and last letter are in the right place. The rest can be a total mess and you can still read it without a problem. This is because we do not read every letter by itself but the word as a whole and the brain figures it out anyway.
Sample run:
This is a test
This is a tset
Hello, how are you today?
Hlleo, how are you toady?
SUGGESTIONS
You may find it easiest to complete this program in stages. Begin by writing a program that echos input lines of text to output. Then add the ability (a function) to break a line of text into individual words, finally add the scrambling feature (another function) for a word.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class WordShuffle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = "";
do
{
System.out.print("Enter a string: ");
input = sc.nextLine().trim();
if(input.equalsIgnoreCase("quit"))
{
System.out.println("Thanks..Goodbye!\n");
System.exit(0);
}
// read a line from keyboard and split the words and insert them in
an array
String[] data = input.trim().split(" ");
// shuffle the words if the length is greater than 3
String result = "";
for(int i = 0; i < data.length; i++)
{
String word = data[i];
if(word.length() > 3)
result += getShuffledWord(word) + " ";
else
result += word + " ";
}
System.out.println("Result string: " + result.trim() + "\n");
}while(!input.equalsIgnoreCase("quit"));
}
private static String getShuffledWord(String str)
{
String res = "";
// array list to hold the characters of the string str
ArrayList<Character> chars = new ArrayList<>();
// a for loop from index 1 to length() - 1 so as to exclude the
first and last letter of the str
// and add the remaining letters to the arraylist
char firstLetter = getFirstLetter(str);
char lastLetter = getLastLetter(str);
char punc = 0;
if(!Character.isLetter(str.charAt(str.length() - 1)))
punc = str.charAt(str.length() - 1);
while(res.equalsIgnoreCase(str))
{
if(punc == 0)
{
// the last letter is not a punctuation
for(int i = 1; i < str.length() - 1; i++)
chars.add(str.charAt(i));
}
else
{
// the last letter is a punctuation
for(int i = 1; i < str.length() - 2; i++)
chars.add(str.charAt(i));
}
// shuffle the arraylist
Collections.shuffle(chars);
// now arrange the letters
res += firstLetter + "";
for (Character c : chars)
res += c;
if(punc != 0)
res += lastLetter + String.valueOf(punc);
else
res += lastLetter;
}
if (punc == 0) {
// the last letter is not a punctuation
for (int i = 1; i < str.length() - 1; i++) {
chars.add(str.charAt(i));
}
} else {
// the last letter is a punctuation
for (int i = 1; i < str.length() - 2; i++) {
chars.add(str.charAt(i));
}
}
// shuffle the arraylist
Collections.shuffle(chars);
// now arrange the letters
res += firstLetter + "";
for (Character c : chars)
res += c;
// if there is a punctuation, add it to the end of the word
if(punc != 0)
res += lastLetter + String.valueOf(punc);
else
res += lastLetter;
return res;
}
private static char getFirstLetter(String s)
{
char res = 0;
for(int i = 0; i < s.length(); i++)
{
if(Character.isLetter(s.charAt(i)))
{
res = s.charAt(i);
break;
}
}
return res;
}
private static char getLastLetter(String s)
{
char res = 0;
for(int i = s.length() - 1; i >= 0; i--)
{
if(Character.isLetter(s.charAt(i)))
{
res = s.charAt(i);
break;
}
}
return res;
}
}

******************************************************** SCREENSHOT ********************************************************