Caesar Cipher

  • + 0 comments
    def caesarCipher(s, k):
        alphabet = 'abcdefghijklmnopqrstuvwxyz'
        result = ''
        
        for char in s:
            if char.lower() in alphabet:
                idx = alphabet.index(char.lower()) + k
                c = alphabet[idx % len(alphabet)]
                
                if char.isupper():
                    result += c.upper()
                else:
                    result += c
                    
            else:
                result += char
                
        return result