strlen函数的使用
时间: 2024-06-17 16:05:19 浏览: 14
strlen函数是一个C语言的字符串函数,用于计算字符串的长度,不包括字符串末尾的'\0'(空字符)。该函数需要一个指向字符串的指针作为参数,返回值为该字符串的长度。
使用方法示例:
```c
#include <string.h>
#include <stdio.h>
int main() {
char str[] = "hello world";
int len = strlen(str);
printf("The length of \"%s\" is %d.\n", str, len);
return 0;
}
```
输出结果为:
```
The length of "hello world" is 11.
```
相关问题
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函数
strlen函数是一个C标准库函数,用于计算字符串的长度。它的参数为一个字符指针,返回值为一个无符号整数,即字符串的长度。使用strlen函数可以方便地计算字符串的长度,而不需要手动遍历字符串。下面是一个使用strlen函数计算字符串长度的示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "hello world";
size_t len = strlen(str);
printf("The length of the string is %zu\n", len);
return 0;
}
```
输出结果为:
```
The length of the string is 11
```