输入一个字符串,加密后输出,加密算法为:如是字母则转换为其后第五个字母,其他字符不变。例如,字母A(或a)加密后变为字母F(或f),字母V(或v)加密后变为字母A(或a)。
def encrypt(s):
result = ''
for char in s:
if char.isalpha():
if char.islower():
result += chr((ord(char) - ord('a') + 5) % 26 + ord('a'))
else:
result += chr((ord(char) - ord('A') + 5) % 26 + ord('A'))
else:
result += char
return result
# 测试
s = input('请输入一个字符串:')
print('加密后的字符串为:', encrypt(s))
input_str = 'a1bvca2z'
output_str = ""
for input_chr in input_str:
if ord('a') <= ord(input_chr) <= ord("z"):
output_str += chr(
ord('a') + (ord(input_chr) + 5 - ord('a')) % 26
)
elif ord('A') <= ord(input_chr) <= ord("Z"):
output_str += chr(
ord('A') + (ord(input_chr) + 5 - ord('A')) % 26
)
else:
output_str += input_chr
print(output_str)