Advertisement
apad464

WordScrambler.java (2014 FRQ)

Feb 27th, 2023 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. class WordScrambler {
  4.     private String[] scrambledWords;
  5.  
  6.     public WordScrambler(String[] wordArr) {
  7.         scrambledWords = mixedWords(wordArr);
  8.     }
  9.  
  10.     private String recombine(String word1, String word2) {
  11.         int fhalf = word1.length() / 2, shalf = word2.length() / 2;
  12.         return word1.substring(0, fhalf) + word2.substring(shalf);
  13.     }
  14.  
  15.     public String[] mixedWords(String[] words) {
  16.         String[] output = new String[words.length];
  17.         for (int i = 0; i < words.length; i += 2) {
  18.             output[i] = recombine(words[i], words[i + 1]);
  19.             output[i + 1] = recombine(words[i + 1], words[i]);
  20.         }
  21.         return output;
  22.     }
  23. }
  24.  
  25. public class WordScramblerRunner {
  26.     public static void main(String[] args) {
  27.         String[] array = {"apple", "pear", "this", "cat"};
  28.         WordScrambler one = new WordScrambler(array);
  29.         System.out.println(Arrays.toString(one.mixedWords(array)));
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement