Caesar Cipher

  • + 0 comments

    def shiftLetter(ch, shift): sub_const = ord('z') - ord('a') + 1 shift = shift % 26 numval = ord(ch)+shift
    upper = ch.isupper()

    if upper:  # case it's uppercase
        if numval > ord('Z'):  # check if have to reset to 1
            numval = numval - sub_const
    else:  # case it's lowercase
        if numval > ord('z'):
            numval = numval -  sub_const
    return chr(numval)
    

    def caesarCipher(s, k): # Write your code here new_s = str() for c in s: if c.isalpha(): ch = shiftLetter(c, k) else: #non alpha chars ch = c new_s += ch return new_s