用户在一行中输入一个包括大小写字母和数字的字符串,编程将其中的大写字母用字母表中该字母后的第5个字母替代,小写字母用字母表中该字母后的第3个字母替代,其他字符原样输出,实现字符串加密。
时间: 2024-04-09 13:28:20 浏览: 10
可以使用循环遍历字符串中的每个字符,判断其是否为大写字母、小写字母或其他字符,然后根据不同情况进行替换或原样输出。具体实现可以参考以下代码:
```
# 输入字符串
s = input("请输入字符串:")
# 定义字母表
alphabet = "abcdefghijklmnopqrstuvwxyz"
# 遍历字符串中的每个字符
for c in s:
# 如果是大写字母,则替换为字母表中该字母后的第5个字母
if c.isupper():
index = alphabet.index(c.lower()) + 5
if index >= len(alphabet):
index -= len(alphabet)
print(alphabet[index].upper(), end="")
# 如果是小写字母,则替换为字母表中该字母后的第3个字母
elif c.islower():
index = alphabet.index(c) + 3
if index >= len(alphabet):
index -= len(alphabet)
print(alphabet[index], end="")
# 其他字符原样输出
else:
print(c, end="")
```
例如,输入字符串为"Hello, World! 123",则输出结果为"Mjqqt, Btwqi! 123",其中大写字母"H"被替换为"M",小写字母"e"被替换为"h",其他字符原样输出。