id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

orion.a.smith@gmail.com apcsa-encryption > apcsaEncryptPairSwap
prev  |  next  |  chance

(MEDIUM) Given an input String, rearrange the letters so that pairs of letters are moved from the back to the front. The first two letters are swapped with the last two, though both pairs stay in their original order. Then the next two letters from each direction (front and back) are swapped, and so on. A String with original index positions of 01234567 becomes 67452301. This encryption scheme is also its own decryption scheme, since running it with previous output will restore the original String. Precondition: input Strings will have a length evenly divisible by 2.

HINT
For many String construction problems, the strategy is to build an output String one piece at a time by concatenating characters or other Strings into it. You can create an empty String like this:
String a = "";
Since the output of this problem is built two characters at a time, maybe you could design a loop and a substring that think about two characters at once?

apcsaEncryptPairSwap("01234567") → "67452301"
apcsaEncryptPairSwap("67452301") → "01234567"
apcsaEncryptPairSwap("PairSwap") → "apSwirPa"

...Save, Compile, Run (ctrl-enter)

public String apcsaEncryptPairSwap(String str) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 320

Copyright Nick Parlante 2017 - privacy