Change each character in string to the next character in alphabet

前端 未结 8 1072
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-03 15:45

I am coding in Python 2.7 using PyCharm on Ubuntu.

I am trying to create a function that will take a string and change each character to the character that would be

8条回答
  •  抹茶落季
    2021-01-03 16:17

    The problem you are solving is of Ceaser cipher. you can implement the formula in your code.

    E(x) = (x+n)%26 where x is your text and n will be the shift.

    Below is my code. (I write the code in python 3)

    import ast
    n = ast.literal_eval(input())
    n1 = n[0]
    step = n[1]
    def enc_dec(string,step):
        result = ''
        for i in string:
            temp = ''
            if i=='':
                result = result+i
            elif i.isupper():
                temp = chr((ord(i) + step - 65) % 26 + 65)
            else:
                temp = chr((ord(i) + step - 97) % 26 + 97)
            result = result + temp
        return result
    print(enc_dec(n1,step))
    

提交回复
热议问题