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

 

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

Given an input String and a number n, convert each char into a binary representation by converting to an int and using the static toString method with two parameters of the Integer class to convert to its binary equivalent ones and zeroes. Then, transpose the last n bits of each character to the character after it, with the String's last character having its bits transposed into the first character. Reassemble the Strings representing binary to characters with Integer.parseInt(String s, int radix) and concatenate into the output. Precondition: str will have at least two characters.


OBSERVATION: The Unicode specification dates back to ASCII, where for English uppercase and lowercase letters only the last five bits change from one letter to another within the same case.  This fact produces some interesting effects with mixed-case inputs: check out the input "AaBbCcDd" using a 5-bit shift, for example.

HINT: here is a code sequence that converts a character of a String to a char, to an int, to a binary String, back to an int, back to a char.
String s = "hello world";
char c0 = s.charAt(0);
int i0 = (int)c0;
String bin0 = Integer.toString(i0, 2);
// could use substring here to get last n bits, transpose
i0 = Integer.parseInt(bin0, 2);
c0 = (char)i0;

apcsaEncryptCycleBits("abcde", 3) → "eabcd"
apcsaEncryptCycleBits("ABCDE", 3) → "EABCD"
apcsaEncryptCycleBits("JKLMN", 3) → "NJKLM"

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

public String apcsaEncryptCycleBits(String str, int n) { }

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: 330

Copyright Nick Parlante 2017 - privacy