编写加密程序,加密规则为:将所有字母转化为该字母后的第三个字母,即A->D、B->E、C->F、…、Y->B、Z->C。小写字母同上,其他字符不做转化。输入任意字符串,输出加密后的结果。
例如:输入"I love 007",输出"L oryh 007"
代码:
#include <iostream>
using namespace std;
int main() {
char str[100];
fgets(str, 100, stdin);
for (int i = 0; str[i] != '\n'; ++i) {
//if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
if (str[i] >= 'a' && str[i] <= 'z') {
int n = str[i] - 'a';
n = (n + 3) % 26;
str[i] = n + 'a';
}
else if (str[i] >= 'A' && str[i] <= 'Z') {
int n = str[i] - 'A';
n = (n + 3) % 26;
str[i] = n + 'A';
}
}
cout << str << endl;
}
运行结果: