strlen 使用例子
时间: 2023-07-22 12:09:30 浏览: 45
下面是一个使用 `strlen` 函数的例子:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, world!";
printf("Length of the string is: %ld\n", strlen(str));
return 0;
}
```
这个程序会输出 `Length of the string is: 13`,因为字符串 "Hello, world!" 的长度是 13 个字符。
相关问题
strlen函数使用例子
strlen函数用于计算字符串的长度,其使用方法如下:
```c++
#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;
return 0;
}
```
输出结果为:
```
The length of the string is: 12
```
其中,`strlen()`函数需要包含头文件`<cstring>`,并且接受一个`const char*`类型的参数,返回一个`size_t`类型的值,表示字符串的长度。在上述例子中,我们定义了一个字符串`str`,并使用`strlen()`函数计算其长度并输出。
strlen c++使用例子
strlen函数是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;
return 0;
}
输出结果为:The length of the string is: 12
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)