strcspn和strlen函数
时间: 2024-03-24 18:32:40 浏览: 46
strcspn和strlen函数是C语言中的字符串处理函数,下面是它们的介绍和演示:
1. strlen函数
函数介绍:strlen函数用于计算字符串的长度,即字符串中字符的个数,不包括字符串末尾的空字符'\0'。
示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "hello world";
int len = strlen(str);
printf("The length of the string is: %d\n", len); // 输出:The length of the string is: 11
return 0;
}
```
2. strcspn函数
函数介绍:strcspn函数用于计算字符串str1中第一个不包含字符串str2中任何字符的位置,返回值为该位置的下标。
示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "hello world";
char str2[] = "aeiou";
int index = strcspn(str1, str2);
printf("The index of the first character that does not appear in str2 is: %d\n", index); // 输出:The index of the first character that does not appear in str2 is: 1
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](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)