Caesar Cipher

  • + 0 comments
    def caesarCipher(s, k):
            alphabet = "abcdefghijklmnopqrstuvwxyz"
            res = ""
            for c in s:
                    is_upper = c.isupper()
                    idx = alphabet.find(c.lower())
                    if idx != -1:
                            new_idx = (idx + k) % 26
                            res += alphabet[new_idx].upper() if is_upper else alphabet[new_idx]
                    else:
                            res += c
    
            return res