c++中求字符串长度的代码
时间: 2023-12-30 14:21:59 浏览: 124
以下是两种C++中求字符串长度的代码: 1.使用string类的length()函数
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello World!";
int len = str.length();
cout << "The length of the string is: " << len << endl; // 输出:The length of the string is: 12
return 0;
}
2.使用C语言库函数strlen()
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[] = "Hello World!";
int len = strlen(str);
cout << "The length of the string is: " << len << endl; // 输出:The length of the string is: 12
return 0;
}
阅读全文
相关推荐















