- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Caesar Cipher in Python
Suppose we have a lowercase alphabet string s, and an offset number say k. We have to replace every letter in s with a letter k positions further along the alphabet. We have to keep in mind that when the letter overflows past a or z, it gets wrapped around the other side.
So, if the input is like "hello", k = 3, then the output will be "khoor"
To solve this, we will follow these steps −
Define a function shift(). This will take c
i := ASCII of (c) - ASCII of ('a')
i := i + k
i := i mod 26
return character from ASCII (ASCII of ('a') + i)
From the main method, do the following −
ret := for each character c in s, make a list of elements by calling shift(c)
Let us see the following implementation to get better understanding −
class Solution: def solve(self, s, k): def shift(c): i = ord(c) - ord('a') i += k i %= 26 return chr(ord('a') + i) return "".join(map(shift, s)) ob = Solution() print(ob.solve("hello", 3))
- Atbash cipher in Python
- Difference between Block Cipher and Stream Cipher
- Difference between Monoalphabetic Cipher and Polyalphabetic Cipher
- C++ Program to Implement Caesar Cypher
- XOR Cipher in C++
- Difference between Substitution Cipher Technique and Transposition Cipher Technique
- Polybius Square Cipher in C++
- Program to encrypt a string using Vertical Cipher in Python
- Program to encrypt a string using Vigenere cipher in Python
- What is the comparison between Stream Cipher and Block Cipher in information security?
- What is Block Cipher in information security?
- What is Stream Cipher in Information Security?
- What is Monoalphabetic Cipher in Information Security?
- C++ Program to Implement Affine Cipher