华为机试题:ord()与chr()的运用,字符串函数 islower(),isalpha(),isupper()函数的运用


'''
对输入的字符串进行加解密,并输出。

加密方法为:

当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B;字母Z时则替换为a;

当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0;

其他字符不做变化。

解密方法为加密的逆过程。
数据范围:输入的两个字符串长度满足 1 \le n \le 1000 \1≤n≤1000 ,保证输入的字符串都是只由大小写字母或者数字组成
输入描述:
第一行输入一串要加密的密码
第二行输入一串加过密的密码

输出描述:
第一行输出加密后的字符
第二行输出解密后的字符

示例1
输入:
abcdefg
BCDEFGH
复制
输出:
BCDEFGH
abcdefg
'''

# a= input()
# b=input()
a = '2OA92AptLq5G1lW8564qC4nKMjv8C'
b = 'B5WWIj56vu72GzRja7j5'
str1 =''
str2 =''
for x in a:
# if ord('a')<=ord(x)<=ord('z') or ord('A')<=ord(x)<=ord('Z'):
if x.isalpha():
if x=='z':
str1=str1+'A'
elif x =='Z':
str1 = str1 + 'a'
else:
if x.islower():
str1 = str1+chr((ord(x)+1)).upper()
else:
str1 = str1 + chr((ord(x) + 1)).lower()
elif ord('0')<=ord(x)<=ord('9'):
if x =='9':
str1 = str1+'0'
else:
str1+=str(int(x)+1)
else:
str1 = str1 + x

for x in b:
# if ord('a')<=ord(x)<=ord('z') or ord('A')<=ord(x)<=ord('Z'):
if x.isalpha():
if x=='A':
str2=str2+'z'
elif x =='a':
str2 = str2 + 'Z'
else:
if x.islower():
str2 = str2+chr((ord(x)-1)).upper()
else:
str2 = str2 + chr((ord(x) -1)).lower()
elif ord('0')<=ord(x)<=ord('9'):
if x =='0':
str2 = str2+'9'
else:
str2+=str(int(x)-1)
else:
str2 = str2 +x
print(str1)
print(str2)