#include <iostream>
#include <cstring> // 包含 strlen 函数的头文件
int main() {
const char* str = "Hello, world!";
// 使用 strlen 函数计算字符串长度
size_t length = strlen(str);
std::cout << "The length of the string \"" << str << "\" is " << length << " characters." << std::endl;
return 0;
}
#include <cstring>
是为了使用 strlen
函数,该函数用于计算以空字符 \0
结尾的字符串的长度。const char* str = "Hello, world!";
定义了一个 C 风格的字符串(以空字符结尾的字符数组)。size_t length = strlen(str);
调用 strlen
函数来计算字符串 str
的长度,并将结果存储在 length
变量中。std::cout
输出字符串及其长度。这个示例展示了如何使用 strlen
函数来获取 C 风格字符串的长度。
上一篇:c++ 分割字符串
下一篇:c++中&是什么意思
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站