Caesar cipher

Report a typo

We have code that takes a secret message as input and shifts each uppercase letter 3 positions down the alphabet. If there are no following letters in the alphabet after the shift, the code wraps around and starts from the first letter of the alphabet.

fun main() {
    val secretMessage = "MXQE!"

    val encryptedMessage = buildString {
        for (char in secretMessage) {
            if (char.isLetter()) {
                val shiftedChar = char.code + 3
                val wrappedChar = if (shiftedChar > 'Z'.code) {
                    'A' + (shiftedChar - 'Z'.code - 1)
                } else {
                    shiftedChar.toChar()
                }
                append(wrappedChar)
            } else {
                append(char)
            }
        }
    }

    println(encryptedMessage)
}

Enter the output of this code in the box below.

Enter a short text
___

Create a free account to access the full topic