Caesar Cipher

  • + 0 comments

    Java solution

    public static String caesarCipher(String s, int k) {
        // Write your code here
        StringBuilder answer = new StringBuilder();
        for(char ch : s.toCharArray()) {
            if(Character.isLetter(ch)) {
                char base = Character.isUpperCase(ch) ? 'A' : 'a';
                answer.append((char)((ch-base+k)%26+base));
            } else {
                answer.append(ch);
            }
            
        }
        return answer.toString();
            
    
        }