c++ - 如何在 C++ 中将字符串中的字符转换为按字母顺序排列的前一个字符

标签 c++ string character alphabetical

如果我输入“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/

相关文章:

c++ - 错误 LNK2001 : unresolved external symbol ___iob_func in VS2017

php - 在字符串中查找第一个大写字符的最快方法

C,当我更改二维数组的单个位置中的字符时,第二个位置的字符也会更改

c++ - 如何将 clang-tidy 集成到 CMake 和 GCC?

c++ - 使用 qt + qprinter 将工作 url 添加到 pdf

c++ - 陷入 while 循环 C++

c - 带有简单计数器的循环发生故障?

java - 字符串中的简单比较

javascript - R 中 Shiny 的 STRING 交互网络

r - 使用 as.character 将表达式强制转换为字符时,有没有办法绕过 500 个字符的限制?