Cipher

in #ita4 months ago

class AtbashCipher {

static String plain =  "abcdefghijklmnopqrstuvwxyz"

static String cipher = "zyxwvutsrqponmlkjihgfedcba"





static String encode(phrase) {

    String res = ""

    int k = 0

    phrase = phrase.toLowerCase()

    for(char lett : phrase.toCharArray()){

        if(Character.isDigit(lett)){

            res += String.valueOf(lett)

            k++

            if(k==5){

                res += " "

                k=0

            }    

        }

        else if (!Character.isLetterOrDigit(lett)|| Character.isWhitespace(lett)){

            continue

        }

        else{

             int ind = plain.indexOf(lett.toString())

             res += String.valueOf(cipher[ind])

             k++

             if(k==5){

                    res += " "

                    k=0

            } 

        }

    }

    

    return res.trim()

}

static String decode(phrase) {

    return encode(phrase).replace(" ", "")

}

}