DEV Community

Lancelot03
Lancelot03

Posted on

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

Top comments (0)