有多種方法可以逆序輸出一個(gè)C++字符串。以下是其中一種方法:
#include <iostream>
#include <string>
int main() {
std::string str = "Hello World";
// 逆序輸出字符串
for (int i = str.length() - 1; i >= 0; i--) {
std::cout << str[i];
}
return 0;
}
在上面的代碼中,我們首先定義了一個(gè)字符串str
,然后使用一個(gè)for
循環(huán)逆序輸出字符串中的每個(gè)字符。輸出結(jié)果將是dlroW olleH
。