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

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. 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 of these problems, the strategy is to build an output String one character at a time by concatenating characters into it. You can create an empty String like this:
String a = "";
For this exercise, you may want to use the charAt() method of a String as well as the length() method. Here's a sample for loop to go through all the characters of a String and add them to an output. (Very close to what this problem requires, but not quite)
public String apcsaEncryptPairSwap(String str) {
  String out = "";
  for (int i = 0; i < str.length(); i++)
  {
      out += str.charAt(i);
  }
  return out;
}

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