DEV Community

Lancelot03
Lancelot03

Posted on

5 2

Rot13

ROT13 is a simple letter substitution cipher that replaces a letter with the letter 13 letters after it in the alphabet. ROT13 is an example of the Caesar cipher.

Create a function that takes a string and returns the string ciphered with Rot13. If there are numbers or special characters included in the string, they should be returned as they are. Only letters from the latin/english alphabet should be shifted, like in the original Rot13 "implementation".

Please note that using encode is considered cheating.

Solution- ###Python

def rot13(message):
    alpha="abcdefghijklmnopqrstuvwxyz"
    string=''
    for i in message:
        if i in alpha:
            if alpha.index(i)<13:
                string += alpha[alpha.index(i)+13]
            else:
                string += alpha[alpha.index(i)-13]
        elif i in alpha.upper():
            if alpha.upper().index(i)<13:
                string += alpha.upper()[alpha.upper().index(i)+13]
            else:
                string += alpha.upper()[alpha.upper().index(i)-13]
        else:
            string +=i
    return string
Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay