Kotlinlearncs.online LogoJava

Practice with Strings

Welcome back! This lesson is entirely focused on one problem: encryption.

We’re going to modify the normal lesson flow. We’ll start with the homework problem at the top. If you’d like to just go at on your own, go for it! And, if you’d like a bit of help, we’ll break it down piece-by-piece below.

Let’s get to it!

Homework: Rot 13 Encryption

Created By: learncs.online Staff
/ Version: 2020.9.0

Encryption is an ancient practice of trying to conceal information by scrambling it. Modern encryption techniques are incredibly strong and mathematically sound. But in the past, simpler and more primitive methods were used.

Let's implement a form of encryption known as a Caesar Cipher, sometimes also known as Rot-13 encryption. (Rot for rotation, and 13 for one amount that you might rotate.) Here is how it works. Given a String and an amount to rotate, we replace each character in the String with a new character determined by rotating the original character in a given array. For example, given the String array "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ", "ABC" rotated 3 would be "DEF", and rotated -1 would be " AB". (Note the space at the end of the character array.)

Declare and implement a function called encrypt that, given a String and an Int amount, returns the passed String "encrypted" by rotating it the given amount. ("Encrypted" is in scare quotes because this is not by any means a strong method of encryption!) If the passed String is null you should return null. Note that rotation may be negative, which will require some additional care.

fun encrypt(input: String?, rotation: Int): String? {
val characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
return input

Rot-13 Part 0: Understanding the Problem
Rot-13 Part 0: Understanding the Problem

Let’s break down this problem into smaller pieces, and spend a few moments just orienting ourselves and figuring out what to do. We won’t write test cases yet, and instead save them for the smaller pieces that we’re about to create.

// Breaking Down Rot-13

Rot-13 Part 1: Character Mapping
Rot-13 Part 1: Character Mapping

Now that we have a sense of what the different pieces are, let’s look at one of the core challenges: remapping each character. We’ll also write some simple test cases for our helper method.

// Remapping Each Character

Rot-13 Part 2: Breaking Down the String
Rot-13 Part 2: Breaking Down the String

At this point we’ve identified how to remap individual characters. Next we need to review how to break the input String into individual characters.

// Breaking a String Into Characters

Rot-13 Part 3: Putting it All Together
Rot-13 Part 3: Putting it All Together

Now that we have our building blocks, let’s integrate everything together!

// Putting it All Together

More Practice
More Practice

If you are enjoying Strings, rotation, and modular arithmetic, and haven’t had enough yet—here is a practice problem that you might enjoy!

Created By: learncs.online Staff
/ Version: 2020.9.1

This problem combines Strings, functions, and arrays. Super fun!

Write a function called rotateRight that takes a nullable String as its first argument and a non-negative Int as its second argument and rotates the String by the given number of characters. Here's what we mean by rotate:

  • CS125 rotated right by 1 becomes 5CS12
  • CS125 rotated right by 2 becomes 25CS1
  • CS125 rotated right by 3 becomes 125CS
  • CS125 rotated right by 4 becomes S125C
  • CS125 rotated right by 5 becomes CS125
  • CS125 rotated right by 8 becomes 125CS

And so on. Notice how characters rotated off the right end of the String wrap around to the left.

This problem is tricky! Here are a few hints:

  1. You will want to use the remainder operator to perform modular arithmetic.
  2. You will probably want to convert the String to an array of characters before you begin.
  3. You can convert an array of characters characters back into a String like this: String(characters).
  4. You can also solve this problem quite elegantly using substring.

If the passed String argument is null, you should return null. Good luck and have fun!

More Practice

Need more practice? Head over to the practice page.