Sha256: 4d969196d1260368ce677f924e8ceac16b1c0b636c9a4c1924b73bc194bc6aa2

Contents?: true

Size: 1.58 KB

Versions: 133

Compression:

Stored size: 1.58 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 String encode(String input) {
        String encoded = stripInvalidCharacters(input).toLowerCase();
        String cyphered = "";

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

        return splitIntoFiveLetterWords(cyphered);
    }

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

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

        return deciphered;
    }

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

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

        return filteredValue;
    }

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

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

    private 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

133 entries across 133 versions & 1 rubygems

Version Path
trackler-2.2.1.42 tracks/java/exercises/atbash-cipher/.meta/src/reference/java/Atbash.java
trackler-2.2.1.41 tracks/java/exercises/atbash-cipher/.meta/src/reference/java/Atbash.java
trackler-2.2.1.40 tracks/java/exercises/atbash-cipher/.meta/src/reference/java/Atbash.java
trackler-2.2.1.39 tracks/java/exercises/atbash-cipher/.meta/src/reference/java/Atbash.java
trackler-2.2.1.38 tracks/java/exercises/atbash-cipher/.meta/src/reference/java/Atbash.java
trackler-2.2.1.37 tracks/java/exercises/atbash-cipher/.meta/src/reference/java/Atbash.java
trackler-2.2.1.36 tracks/java/exercises/atbash-cipher/.meta/src/reference/java/Atbash.java
trackler-2.2.1.35 tracks/java/exercises/atbash-cipher/.meta/src/reference/java/Atbash.java
trackler-2.2.1.34 tracks/java/exercises/atbash-cipher/.meta/src/reference/java/Atbash.java
trackler-2.2.1.33 tracks/java/exercises/atbash-cipher/.meta/src/reference/java/Atbash.java
trackler-2.2.1.32 tracks/java/exercises/atbash-cipher/.meta/src/reference/java/Atbash.java
trackler-2.2.1.31 tracks/java/exercises/atbash-cipher/.meta/src/reference/java/Atbash.java
trackler-2.2.1.30 tracks/java/exercises/atbash-cipher/.meta/src/reference/java/Atbash.java
trackler-2.2.1.29 tracks/java/exercises/atbash-cipher/.meta/src/reference/java/Atbash.java
trackler-2.2.1.28 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.2.1.27 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.2.1.26 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.2.1.25 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.2.1.24 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java
trackler-2.2.1.23 tracks/java/exercises/atbash-cipher/src/example/java/Atbash.java