题解 | #密码翻译#
密码翻译
https://www.nowcoder.com/practice/136de4a719954361a8e9e41c8c4ad855
#include<iostream>
#include<string>
using namespace std;
int main() {
//学习getline()
//题目指定是读取一行字符串
//如果直接按照string进行cin,遇到空格就会停止输入,与题意不符
//因此要用getline()函数
string str;
while (getline(cin,
str)) { //cin是字符流istream & ; str 是string & (对应的形参)
for (int i = 0; i < str.length(); i++) {
if (str[i] == 'z' || str[i] == 'Z') {
str[i] -= 25;
} else if (('a' <= str[i] && str[i] < 'z') || ('A' <= str[i] && str[i] < 'Z')) {
//Caution!!!!!
//不能用连续的< <=这些,因为会把上一个的判断结果计算为0,1再比较
str[i] += 1;
}
}
cout << str << endl;
}
return 0;
}
注意:
- 不等式不能连续判断
- getline()的用法
查看7道真题和解析