Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- class WordScrambler {
- private String[] scrambledWords;
- public WordScrambler(String[] wordArr) {
- scrambledWords = mixedWords(wordArr);
- }
- private String recombine(String word1, String word2) {
- int fhalf = word1.length() / 2, shalf = word2.length() / 2;
- return word1.substring(0, fhalf) + word2.substring(shalf);
- }
- public String[] mixedWords(String[] words) {
- String[] output = new String[words.length];
- for (int i = 0; i < words.length; i += 2) {
- output[i] = recombine(words[i], words[i + 1]);
- output[i + 1] = recombine(words[i + 1], words[i]);
- }
- return output;
- }
- }
- public class WordScramblerRunner {
- public static void main(String[] args) {
- String[] array = {"apple", "pear", "this", "cat"};
- WordScrambler one = new WordScrambler(array);
- System.out.println(Arrays.toString(one.mixedWords(array)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement