Guest User

E240

a guest
Nov 22nd, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /**
  4.  * Created by Indrid on 11/15/2015.
  5.  */
  6. public class E240 {
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner inputScanner = new Scanner(System.in);
  10.         System.out.print("Input the string\n>> ");
  11.  
  12.         System.out.println(stringParser(inputScanner.nextLine()));
  13.     }
  14.  
  15. //     Takes a sentence string and returns the scrambled string
  16.     static String stringParser(String input) {
  17.         StringTokenizer st = new StringTokenizer(input, " ");
  18.         String finalOutput = ""; // Final shuffled sentence
  19.  
  20.         while (st.hasMoreTokens()) {
  21.             String word = st.nextToken();
  22.             char wordBuffer[] = new char[word.length()]; // Temp storage as word is shuffled
  23.             String stringToBeShuffled = ""; // String excluding special characters to be shuffled
  24.  
  25.             // Stores non-alpha characters in their corresponding places into the wordBuffer char array
  26.             // Stores alpha characters the "stringToBeBuffer" string
  27.             for (int i = 0; i < word.length(); i++) {
  28.                 wordBuffer[i] = ' ';
  29.  
  30.                 if ((word.charAt(i) >= 'a' && word.charAt(i) <= 'z') || (word.charAt(i) >= 'A' && word.charAt(i) <= 'Z')) {
  31.                     stringToBeShuffled += word.charAt(i);
  32.                 } else {
  33.                     wordBuffer[i] = word.charAt(i);
  34.                 }
  35.             }
  36.  
  37.             // Places the scrambled alpha characters back into the open spaces on the
  38.             String tempStr = wordScrambler(stringToBeShuffled);
  39.             for (int i = 0, j = 0; i < word.length(); i++) {
  40.                 if (wordBuffer[i] == ' ') {
  41.                     wordBuffer[i] = tempStr.charAt(j);
  42.                     j++;
  43.                 }
  44.             }
  45.  
  46.             finalOutput += String.valueOf(wordBuffer) + " ";
  47.         }
  48.  
  49.         return finalOutput;
  50.     }
  51.  
  52.     static String wordScrambler(String input) { // The person reading this code is lucky I didn't scramble the function name
  53.         String output = "";
  54.         int strLen = input.length();
  55.  
  56.         if (strLen > 3) {
  57.             output += input.charAt(0);
  58.             output += shuffleString(input.substring(1, strLen - 1));
  59.             output += input.charAt(strLen - 1);
  60.         } else {
  61.             output = input;
  62.         }
  63.  
  64.         return output;
  65.     }
  66.  
  67.     // Shuffles the inputted string excluding the first and last characters. Guarantees a different word for words with length > 3
  68.     static String shuffleString(String string) {
  69.         String shuffled;
  70.         do {
  71.             shuffled = "";
  72.             List<String> letters = Arrays.asList(string.split(""));
  73.             Collections.shuffle(letters);
  74.             for (String letter : letters) {
  75.                 shuffled += letter;
  76.             }
  77.         } while (shuffled.equals(string) && shuffled.length() > 1
  78.                 && shuffled.charAt(0) != shuffled.charAt(1)); // Only get's checked if above isn't true
  79.  
  80.         return shuffled;
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment