Caesar Cipher

  • + 0 comments

    Simplified Java Solution Method:

    public static String caesarCipher(String s, int k) {
        // Write your code here
            String cipher = "";
            k%=26;
                    
            for(char c : s.toCharArray()){
                if(Character.isLetter(c)){
                    if((Character.isUpperCase(c)&&(c+k)>90)||(Character.isLowerCase(c)&&(c+k)>122)){
                         c-=26;                  
                    }
                    c+=k;
                }
                cipher+=c;   
            }
            return cipher;
        }