题解 | #字符串加解密#

字符串加解密

https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a


def encrypt(s1):
    s1_encrypt=''    #加密结果
    for i in s1:
        if i.isupper():
            if ord(i)==90:
                s1_encrypt += chr(ord('a'))
            else:
                s1_encrypt += chr(1+ord(i.lower()))
        elif i.islower():
            if ord(i)==122:
                s1_encrypt += chr(ord('A'))
            else:
                s1_encrypt += chr(1+ord(i.upper()))
        elif i.isdigit():
            if ord(i)==57:
                s1_encrypt += chr(ord('0'))
            else:
                s1_encrypt += chr(1+ord(i))
        else:
            s1_encrypt += i
            
    print(s1_encrypt)

def decrypt(s2):
    s2_decrypt=''  #解密结果
    for i in s2:
        if i.isupper():
            if ord(i)==65:
                s2_decrypt += chr(ord('z'))
            else:
                s2_decrypt += chr(ord(i.lower())-1)
        elif i.islower():
            if ord(i)==97:
                s2_decrypt += chr(ord('Z'))
            else:
                s2_decrypt += chr(ord(i.upper())-1)
        elif i.isdigit():
            if ord(i)==48:
                s2_decrypt += chr(ord('9'))
            else:
                s2_decrypt += chr(ord(i)-1)
        else:
            s2_decrypt += i
            
    print(s2_decrypt)

while 1:
    try:
        s1=input()
        s2=input()
        encrypt(s1)
        decrypt(s2)
    except:
        break







全部评论

相关推荐

10-07 23:57
已编辑
电子科技大学 Java
八街九陌:博士?客户端?开发?啊?
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务