• + 1 comment

    Java

    public static String caesarCipher(String s, int k) {
        // Write your code here
            StringBuilder sb = new StringBuilder(s);
            for(int i=0;i<sb.length();i++) {
                int curr = sb.charAt(i);
                if(curr >= 65 && curr <=90) {
                    int mod = ((curr % 64) + k) % 26;
                    if(mod==0) mod = 26; // for Z
                    sb.setCharAt(i, (char) (64+mod));
                } else if(curr >= 97 && curr <=122) {
                    int mod = ((curr % 96) + k) % 26;
                    if(mod==0) mod = 26;  //for z
                    System.out.println("i: "+i+", mod: "+mod);
                    sb.setCharAt(i, (char) (96+mod));
                }
            }
            return new String(sb);
        }