strcspn() 函数在 C 中
strcspn() 函数计算在两个字符串中匹配到第一个字符之前的字符数。此函数在 “string.h” 头文件中声明。它返回在第一个字符串中第一个匹配字符之前的所有字符数。
以下是 C 语言中 strcspn() 的语法,
size_t strcspn(const char *string1, const char *string2)
此处,
string1 − 要扫描的第一个字符串。
string2 − 用于在第一个字符串中搜索匹配字符的第二个字符串。
以下是 C 语言中 strcspn() 的示例,
示例
#include<stdio.h>
#include<string.h>
int main() {
char str1[] = "Helloworld!";
char str2[] = "work";
int result = strcspn(str1, str2);
printf("Number of characters before matching character : %d
", (result+1));
return 0;
}输出
Number of characters before matching character : 5
在上述程序中,声明了两个 char 类型数组,并将字符串传递给它们。strcspn() 函数正在计算第一个匹配为 “wor” 的字符之前的字符数。因此,在第一个字符串中,有 5 个字符不匹配。因此,输出为 5,该输出存储在变量 result 中。
char str1[] = "Helloworld!"; char str2[] = "work"; int result = strcspn(str1, str2);
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP