Q. In Caesar cipher,
each letter is replaced by another letter which occurs at the d-th
position (when counted from the position of the original letter), in
the English alphabet. For identifying the position of a letter, we
follow the usual order of the English alphabet, from a to z. Given a word and a
positive integer d, use Caesar cipher to encrypt it. For example, if the word
is 'ball' and the value of 'd' is 3 then the new encrypted word is 'edoo'. 'x'
will be replaced by 'a', 'y' should be replaced by 'b' and 'z' should be
replaced by 'c'. While the code is submitted for Online Judge (SkillRack), use
rstrip(), to remove carriage return character in the input.
Input Format
Word
A positive integer 'd'
Output format:
Encrypted word
Input:
The
word(word)
A
positive number(number)
Processing:
word.rstrip()
number
= int(input())
number
= abs(number)
number
= number%26
code
= []
for
i in range(len(word)-1):
temp = ord(word[i])+ number
if temp>122:
temp = temp - 122
temp = 96 + temp
code.insert(i,chr(temp))
else:
code.insert(i,chr(temp))
Output:
Display
the encrypted word
Algorithm:
Step1.
Get the word from the user.
Step2.
Remove the carriage from the word
Step3.
Get a positive integer
Step4.
Find the positive equivalent of the number if not positive
Step5.
Change number as the remainder when number is divided by 26
Step6.
Initialize code as an empty list and i as 0.
Step7.
Repeat till i is less than length of the word
Step7.1
assign temp as ord(word[i]) + number ord() is used to covert the character in
its decimal equivalent using ASCII or UNICODE conversions.
Step7.2
check if temp is greater than 122 if true proceed to 7.2.1 else move on to step
7.3
Step7.2.1
assign temp as the difference between temp and 122
Step7.2.2
assign temp as the sum of temp and 96
Step7.2.3
insert the character equivalent of temp in the ith index of code.
Step7.3
if temp is not greater than 122 then insert the character equivalent of temp at
the ith index of code.
Step8.
Display the encrypted code.
Program:
word
= input()
word.rstrip()
number
= int(input())
number
= abs(number)
number
= number%26
code
= []
for
i in range(len(word)):
temp = ord(word[i])+ number
if temp>122:
temp = temp - 122
temp = 96 + temp
code.insert(i,chr(temp))
else:
code.insert(i,chr(temp))
for
i in code:
print(i,end='')Issue:
If the expected output does not come then just change 'for i in range(len(word)):'
to for i in range(len(word)-1):
Flowchart:
To edit this flowchart click on this
No comments:
Post a Comment