新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

c++中如何求字符串长度,C中如何求字符串的反向输出

发布时间:2024-03-28 10:23:11

c++中如何求字符串长度

在C++中,可使用std::string类的size()方法还是length()方法来获得字符串的长度。示例代码以下:

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    
    int length1 = str.size();
    int length2 = str.length();
    
    std::cout << "Length of the string is: " << length1 << std::endl;
    std::cout << "Length of the string is: " << length2 << std::endl;
    
    return 0;
}

以上代码会输出字符串Hello, World!的长度,即13