Sha256: 8cc736d342f1e854d1b950737bf8bb44fe5a15af313dbcb91e9e1af8915bc48f

Contents?: true

Size: 1.62 KB

Versions: 147

Compression:

Stored size: 1.62 KB

Contents

import java.util.ArrayList;
import java.util.List;

public class Atbash {

    private static final int GROUP_SIZE = 5;
    private static final String PLAIN = "abcdefghijklmnopqrstuvwxyz";
    private static final String CIPHER = "zyxwvutsrqponmlkjihgfedcba";

    public static String encode(String input) {
        String encoded = stripInvalidCharacters(input).toLowerCase();
        String cyphered = "";

        for (char c : encoded.toCharArray()) {
            cyphered += applyCipher(c);
        }

        return splitIntoFiveLetterWords(cyphered);
    }

    public static String decode(String input) {
        String encoded = stripInvalidCharacters(input).toLowerCase();
        String deciphered = "";

        for (char c : encoded.toCharArray()) {
            deciphered += applyCipher(c);
        }

        return deciphered;
    }

    private static String stripInvalidCharacters(String input) {
        String filteredValue = "";

        for (char c : input.toCharArray()) {
            if (Character.isLetterOrDigit(c)) {
                filteredValue += c;
            }
        }

        return filteredValue;
    }

    private static char applyCipher(char input) {
        int idx = PLAIN.indexOf(input);

        return idx >= 0 ? CIPHER.toCharArray()[idx] : input;
    }

    private static String splitIntoFiveLetterWords(String value) {
        List<String> words = new ArrayList<>();

        for (int i = 0; i < value.length(); i += GROUP_SIZE) {
            words.add(i + GROUP_SIZE <= value.length() ? value.substring(i, i + GROUP_SIZE) : value.substring(i));
        }

        return String.join(" ", words);
    }
}

Version data entries

147 entries across 147 versions & 1 rubygems

Version Path
trackler-2.0.8.47 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.0.8.46 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.0.8.45 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.0.8.44 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.0.8.43 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.0.8.42 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.0.8.41 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.0.8.40 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.0.8.39 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.0.8.38 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.0.8.37 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.0.8.36 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.0.8.35 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.0.8.34 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.0.8.33 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.0.8.32 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.0.8.31 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.0.8.30 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.0.8.29 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.0.8.28 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java