Caesar cipher python with alphabet input

The Caesar cipher is a simple encryption technique that shifts the letters of the alphabet by a certain number of positions. In this case, we will implement a Caesar cipher in Python that accepts an alphabet input. The goal is to encrypt a given message using the provided alphabet.

Option 1: Using a Dictionary

One way to solve this problem is by using a dictionary to map each letter of the alphabet to its corresponding shifted letter. Here’s how we can implement it:


def caesar_cipher(message, alphabet):
    cipher = {}
    for i in range(len(alphabet)):
        cipher[alphabet[i]] = alphabet[(i + 1) % len(alphabet)]
    
    encrypted_message = ""
    for letter in message:
        if letter in cipher:
            encrypted_message += cipher[letter]
        else:
            encrypted_message += letter
    
    return encrypted_message

alphabet = "abcdefghijklmnopqrstuvwxyz"
message = "hello world"
encrypted_message = caesar_cipher(message, alphabet)
print(encrypted_message)

In this code, we first create a dictionary called “cipher” where each letter of the alphabet is mapped to its shifted letter. We then iterate over the message and replace each letter with its corresponding shifted letter if it exists in the cipher dictionary. If a letter is not in the dictionary (e.g., a space or punctuation), we keep it as is. Finally, we return the encrypted message.

Option 2: Using ASCII Values

Another approach is to use the ASCII values of the letters to perform the shift. Here’s how we can do it:


def caesar_cipher(message, alphabet):
    encrypted_message = ""
    for letter in message:
        if letter.isalpha():
            ascii_value = ord(letter.lower())
            shifted_ascii_value = (ascii_value - ord('a') + 1) % len(alphabet)
            encrypted_message += alphabet[shifted_ascii_value]
        else:
            encrypted_message += letter
    
    return encrypted_message

alphabet = "abcdefghijklmnopqrstuvwxyz"
message = "hello world"
encrypted_message = caesar_cipher(message, alphabet)
print(encrypted_message)

In this code, we iterate over each letter in the message. If the letter is alphabetic, we convert it to lowercase and calculate its ASCII value. We then subtract the ASCII value of ‘a’ and add 1 to get the shifted ASCII value. Finally, we use this shifted ASCII value to retrieve the corresponding letter from the alphabet. If the letter is not alphabetic, we keep it as is.

Option 3: Using String Manipulation

Alternatively, we can solve this problem by manipulating the string directly. Here’s how:


def caesar_cipher(message, alphabet):
    encrypted_message = ""
    for letter in message:
        if letter.isalpha():
            shifted_index = (alphabet.index(letter.lower()) + 1) % len(alphabet)
            encrypted_message += alphabet[shifted_index]
        else:
            encrypted_message += letter
    
    return encrypted_message

alphabet = "abcdefghijklmnopqrstuvwxyz"
message = "hello world"
encrypted_message = caesar_cipher(message, alphabet)
print(encrypted_message)

In this code, we iterate over each letter in the message. If the letter is alphabetic, we find its index in the alphabet string and add 1 to get the shifted index. Finally, we use this shifted index to retrieve the corresponding letter from the alphabet. If the letter is not alphabetic, we keep it as is.

After analyzing the three options, the best approach depends on the specific requirements of the problem and personal preference. Option 1 using a dictionary provides a clear mapping between letters and their shifted counterparts, making it easier to understand and modify. Option 2 using ASCII values offers a more mathematical approach and can be useful when dealing with non-alphabetic characters. Option 3 using string manipulation is concise and straightforward, but it may be less efficient for larger alphabets. Ultimately, the choice should be based on the specific needs of the project.

Rate this post

8 Responses

  1. Option 2: Using ASCII Values seems like a cool and nerdy way to implement Caesar cipher in Python. Whos with me? 🤓

    1. I disagree. While Option 2 may seem efficient at first glance, it can easily become convoluted and prone to errors. I prefer Option 1 as it offers a more intuitive and straightforward approach. But hey, to each their own!

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents