要反轉字符串,可以使用以下步驟:
以下是一個示例代碼,用于反轉字符串:
#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