How to Encrypt Words in Java

The challenge

You want to create secret messages which must be deciphered.

Here are the conditions:

  1. Your message is a string containing space separated words.
  2. You need to encrypt each word in the message using the following rules:
    • The first letter must be converted to its ASCII code.
    • The second letter must be switched with the last letter
  3. Keepin’ it simple: There are no special characters in the input.


EncryptWords.encryptThis("Hello") => "72olle"
EncryptWords.encryptThis("good") => "103doo"
EncryptWords.encryptThis("hello world") => "104olle 119drlo"

The solution in Java code

Option 1:

import java.util.Arrays;
import java.util.Iterator;

public class EncryptWords {
    public static String encryptThis(String text) {
        if (text.length()==0) return "";

        String[] words = text.split(" ");

        StringBuilder sb = new StringBuilder();

        for (Iterator<String> it =; it.hasNext(); ) {
            String word =;
            StringBuilder nw = new StringBuilder();

            //first char

            if (word.length()>1) {
                //last char
                nw.append(word.charAt(word.length() - 1));

                //rest of word
                if (word.length()>=3) nw.append(word.substring(2, word.length() - 1));

                //second char
                if (word.length()>2) nw.append(word.charAt(1));

            //add to main word
            sb.append(nw).append(" ");
        return sb.toString().trim();

Option 2:

import java.util.Arrays;

public class EncryptWords {
    public static String encryptThis(String text) {
        return" "))
                .map(w->w.length()>2?(int)w.charAt(0)+w.substring(w.length()-1)+w.substring(2, w.length()-1)+w.substring(1,2):
                .collect(Collectors.joining(" "));

Option 3:

import java.util.Arrays;

public class EncryptWords {
    public static String encryptThis(String text) {

        return text.length() == 0 ? text :" "))
                .map(s -> "" + (int) s.charAt(0)
                        + (s.length() > 2 ? s.charAt(s.length() - 1)
                        + s.substring(2, s.length() - 1)
                        + s.charAt(1) : s.substring(1)))
                .collect(Collectors.joining(" "));

Test cases to validate our solution

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class EncryptWordsTest {
    public void exampleTests() {
        assertEquals("", EncryptWords.encryptThis(""));
        assertEquals("65 119esi 111dl 111lw 108dvei 105n 97n 111ka", EncryptWords.encryptThis("A wise old owl lived in an oak"));
        assertEquals("84eh 109ero 104e 115wa 116eh 108sse 104e 115eokp", EncryptWords.encryptThis("The more he saw the less he spoke"));
        assertEquals("84eh 108sse 104e 115eokp 116eh 109ero 104e 104dare", EncryptWords.encryptThis("The less he spoke the more he heard"));
        assertEquals("87yh 99na 119e 110to 97ll 98e 108eki 116tah 119esi 111dl 98dri", EncryptWords.encryptThis("Why can we not all be like that wise old bird"));
        assertEquals("84kanh 121uo 80roti 102ro 97ll 121ruo 104ple", EncryptWords.encryptThis("Thank you Piotr for all your help"));