以下是使用 Python 語言實現的程式:
python
複製程式碼
def reverse_letter(char):
if 'A' <= char <= 'Z':
return chr(ord('Z') - (ord(char) - ord('A')))
elif 'a' <= char <= 'z':
return chr(ord('z') - (ord(char) - ord('a')))
else:
return char
def encrypt_message(message):
encrypted_message = ''.join(reverse_letter(char) for char in message)
return encrypted_message
# 讓使用者輸入語句
message = input("請輸入一語句: ")
encrypted_message = encrypt_message(message)
# 輸出加密後的語句
print("加密後的語句:", encrypted_message)
程式解釋
reverse_letter 函式:
該函式接受一個字元作為參數。
如果字元是大寫字母(介於 'A' 和 'Z' 之間),則計算其反序字母並返回。
如果字元是小寫字母(介於 'a' 和 'z' 之間),則計算其反序字母並返回。
如果字元不是字母,則原樣返回。
encrypt_message 函式:
該函式接受一個字符串作為參數。
使用列表生成式(list comprehension)遍歷字符串中的每個字元,並將每個字元通過 reverse_letter 函式進行處理。
將處理後的字元列表連接成一個新的字符串並返回。
主程式:
提示使用者輸入一個語句。
調用 encrypt_message 函式對輸入的語句進行加密。
輸出加密後的語句。