Caesar Cipher

  • + 0 comments
    def caesarCipher(s, k):
        # Write your code here
    
        k = k - ((k//26) * 26)    # normalising the K
        cipher = ""
        for i in range(len(s)):
            if 64 < ord(s[i]) < 91 :
                val = ord(s[i]) + k if (ord(s[i]) + k) < 91 else 64 + (ord(s[i]) + k) - 90
                cipher = cipher + chr(val)
            elif 96 < ord(s[i]) < 123 :
                val = ord(s[i]) + k if (ord(s[i]) + k) < 123 else 96 + (ord(s[i]) + k) - 122
                cipher = cipher + chr(val)
            else :
                val = ord(s[i])
                cipher = cipher + chr(val)   
        return cipher