-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25027凱薩.cpp
38 lines (31 loc) · 952 Bytes
/
25027凱薩.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# cs50-problem-set-2-caesar-25027
cs50-problem-set-2-caesar-25027 created by GitHub Classroom
#include <iostream>
using namespace std;
string caesar(int key, string input);
int main() {
int key;
cout << "請輸入密鑰:";
cin >> key;
string plaintext;
cout << "請輸入明文:";
cin >> plaintext;
string ciphertext = caesar(key, plaintext);
cout << "密文為:" << ciphertext;
}
string caesar(int key, string plaintext) {
for (int i = 0; plaintext[i] != '\0'; i++) {
if (plaintext[i] >= 'A' && plaintext[i] <= 'Z') { // 大寫
plaintext[i] += key;
if (plaintext[i] > 'Z') {
plaintext[i] -= 26;
}
} else if (plaintext[i] >= 'a' && plaintext[i] <= 'z') { // 小寫
plaintext[i] += key;
if (plaintext[i] > 'z') {
plaintext[i] -= 26;
}
}
}
return plaintext;
}