温馨提示×

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

c++
小亿
90
2024-05-09 11:39:48
栏目: 编程语言

有多种方法可以逆序输出一个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;
}

在上面的代码中,我们首先定义了一个字符串str,然后使用一个for循环逆序输出字符串中的每个字符。输出结果将是dlroW olleH

0