如果我输入“ABCD”,我希望输出为“ZABC”。 如果我输入“hello world”,输出必须是“gdkkn vnqkc”。
我这样做了:
cout << "Enter text" << endl;
cin >> input;`
result_string = ConvertString(input);
cout << endl << "The result is " << endl << result_string << endl;
但是我不知道如何定义ConvertString
。
谢谢你的帮助。 :)
最佳答案
您必须访问字符串中的每个单独的字符。 String 允许您随机访问内存,因此您可以使用 for 循环来实现。
string input;
cout << "Enter text" << endl;
cin >> input;
//traverse the string
for (int i = 0;i < input.size(); i++)
{
//check for A or a and move it to Z or z respectively
if(input[i] == 65 || input[i] == 97)
input[i] += 26;
//change the value to minus one
input[i]--;
}
cout << endl << "The result is " << endl << input << endl;
return 0;
关于c++ - 如何在 C++ 中将字符串中的字符转换为按字母顺序排列的前一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32940757/