Caesar Cipher

  • + 0 comments

    Java solution:

    public static String caesarCipher(String s, int k) {
    
      char[] chars = s.toCharArray();
      for(int i=0; i<chars.length;i++){ 
          if (chars[i]-'a'>=0 && chars[i]-'a'<26) 
              chars[i]=(char)((chars[i]-'a'+k)%26+'a');                      
          else if (chars[i]-'A'>=0 && chars[i]-'A'<26) 
              chars[i]=(char)((chars[i]-'A'+k)%26+'A');          
    
      }
      return String.valueOf(chars);
    }