Skip to main content
Copy edited (e.g. ref. <https://en.wikipedia.org/wiki/ASCII>).
Source Link

Caesar Ciphercipher using Ascii charASCII character codes

This function should take in a string and a number as paramsparameters and 'rotate' the string to the equivalent alphabet letter that is located at the letter position + 'rotation' parameter.

eE.g.: "stackoverflow" --- "fgnpxbiresybj"

def caesar(inputString, rotation = 1):
    zNum = ord('z')
    alphaLen = 26
    
    result = []
    for c in inputString:
        charNum = ord(c) + rotation
        if charNum > zNum:
            result.append(chr(charNum - alphaLen))
        else:
            result.append(chr(charNum))
            
    return ''.join(result) 


Caesar Cipher using Ascii char codes

This function should take in a string and a number as params and 'rotate' the string to the equivalent alphabet letter that is located at the letter position + 'rotation' parameter.

e.g.: "stackoverflow" --- "fgnpxbiresybj"

def caesar(inputString, rotation = 1):
    zNum = ord('z')
    alphaLen = 26
    
    result = []
    for c in inputString:
        charNum = ord(c) + rotation
        if charNum > zNum:
            result.append(chr(charNum - alphaLen))
        else:
            result.append(chr(charNum))
            
    return ''.join(result)

Caesar cipher using ASCII character codes

This function should take in a string and a number as parameters and 'rotate' the string to the equivalent alphabet letter that is located at the letter position + 'rotation' parameter.

E.g.: "stackoverflow" --- "fgnpxbiresybj"

def caesar(inputString, rotation = 1):
    zNum = ord('z')
    alphaLen = 26

    result = []
    for c in inputString:
        charNum = ord(c) + rotation
        if charNum > zNum:
            result.append(chr(charNum - alphaLen))
        else:
            result.append(chr(charNum))

    return ''.join(result) 


Tweeted twitter.com/StackCodeReview/status/1394397335745138696
Became Hot Network Question
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Source Link
uber
  • 261
  • 2
  • 9

Caesar Cipher using Ascii char codes

This function should take in a string and a number as params and 'rotate' the string to the equivalent alphabet letter that is located at the letter position + 'rotation' parameter.

e.g.: "stackoverflow" --- "fgnpxbiresybj"

def caesar(inputString, rotation = 1):
    zNum = ord('z')
    alphaLen = 26
    
    result = []
    for c in inputString:
        charNum = ord(c) + rotation
        if charNum > zNum:
            result.append(chr(charNum - alphaLen))
        else:
            result.append(chr(charNum))
            
    return ''.join(result)