要反转字符串,可以使用以下步骤:
以下是一个示例代码,用于反转字符串:
#include <iostream>
#include <string>
std::string reverseString(std::string str) {
int n = str.length();
std::string reversedStr = "";
for (int i = n-1; i >= 0; i--) {
reversedStr += str[i];
}
return reversedStr;
}
int main() {
std::string str = "Hello, World!";
std::string reversedStr = reverseString(str);
std::cout << "Original string: " << str << std::endl;
std::cout << "Reversed string: " << reversedStr << std::endl;
return 0;
}
输出结果将为:
Original string: Hello, World!
Reversed string: !dlroW ,olleH