Caesar Cipher

  • + 0 comments

    Python solution:

    def caesarCipher(s, k):
        # Write your code here
        rotatedString=""
        allApha="abcdefghijklmnopqrstuvwxyz"
        for string in s:
            if string.isalpha():
                newIndx = allApha.index(string.lower()) + k
                if newIndx >= 26:
                    newIndx = newIndx % 26 
                newChar= allApha[newIndx].upper() if string.isupper() else allApha[newIndx]
                rotatedString+= newChar
            else:
                rotatedString += string
        return rotatedString