溫馨提示×

c++字符串怎么逆序輸出

c++
小億
144
2024-05-09 11:39:48
欄目: 編程語言

有多種方法可以逆序輸出一個(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。

0