question archive Caesar Cipher function

Caesar Cipher function

Subject:Computer SciencePrice:4.86 Bought8

Caesar Cipher function.

  • takes two inputs, a string for the message, and an integer for the shift
  • returns a string that's just the input string with all of its characters shifted by the amount specified in the shift argument
  • for each character in the string (including punctuation marks, special characters and capital letters) gets that character's Unicode v, and replaces it by the character with the Unicode v + shift
  • accounts for user inputs that aren't strings
  • is as elegant and efficient as possible

 

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Here, Iam giving two solutions as of having small doubt in question(whether 'z' should be rounded to 'a' or not). I get this doubt as you given include Punctuation, special character because there is no rounding for these characters(like 'z' to 'a'). I am apologizing you for this and depends upon your requirement choose the appropriate answer.

  1. Gets UNICODE of any character and shifts that character
  2. Gets UNICODE but in the case of 'Z' or 'z' it starts again from 'A' or 'a'.

 

Program1:

 

def caesarcipher(st,shift):
    l=[]
    for i in range(len(st)):
        l.append(chr((ord(st[i]) + shift)))
    return "".join(l) 

st=input()
shift=int(input())
print(caesarcipher(st,shift))

 

 

Program2:

def caesarcipher(st,shift):
    l=[]
    
    for i in range(len(st)):
        if (st[i].isupper()): 
            l.append(chr((ord(st[i]) + shift-65) % 26 + 65))
        elif(st[i].islower()):
            l.append(chr((ord(st[i]) + shift-97) % 26 + 97))
        else:
            l.append(chr((ord(st[i]) + shift)))
    return "".join(l) 

st=input()
shift=int(input())
print(caesarcipher(st,shift))

 

Both the programs will give you correct output

Step-by-step explanation

Explaining according to the question:

 

1.takes two inputs, a string for the message, and an integer for the s

st=input()
shift=int(input())

st is the input of string and shift is input of number shift used in caesar cipher.

 

2.for each character in the string (including punctuation marks, special characters and capital letters) gets that character's Unicode v, and replaces it by the character with the Unicode v + shift

for i in range(len(st)):
        l.append(chr((ord(st[i]) + shift)))
    return "".join(l) 

 

For each character in the string irrespective of any character, we get UNICODE of that character using ord() and shift that unicode by number shift.

 

ord() - Many people think ord() returns the ASCII value of that character but ASCII is the superset of UNICODE. But ord() returns the UNICODE of the character and chr() returns the character from UNICODEa

chr((ord(st[i]) + shift))

 

ord(st[i])- gives UNICODE of character

ord(st[i])+shift - shift the character for encryption

chr(ord(st[i])+shift ) - returns the encrypted character

 

Same in the case of program 2:

chr((ord(st[i]) + shift-65) % 26 + 65)

Actually UNICODE of 'A' starts from 65.

for example, in case of 'X' and shift=2

ord('X')=88

ord('X')+shift=90

(ord('X')+shift-65)%26= index of character in (A-Z)=25

chr(25+65)='Z'

 

and returning the output, initially I taken a list and appending each encrypted character and joined them using join function.

Please see the attached file for the complete solution

Related Questions