strcspn()函数

函数描述:

检索字符串 str1 开头连续有几个字符都不含字符串 str2 中的字符。

 

函数声明:

#include<string.h>
size_t strcspn(const char* str1, const char *str2);

 

参数:

str1:要被检索的字符串

str2:该字符串包含了要在str1中进行匹配的字符列表。

 

返回值:

成功:返回str1开头连续都不包含str2中字符的字符数。

失败:返回字符串str1的长度。

 

函数功能:

str1为参照,比较字符串str2中的字符是否与str中某个字符相等(也就是检索str2中的字符是否在str1中存在),如果第一次发现相等,则停止并返回在str1中这个匹配相等的字符的索引值,失败则返回str1的长度。

 

实例:

/*
strcspn.c
*/
#include<stdio.h>
#include<string.h>

int main()
{
    int len;
    const char str1[] = "ABCDEFG4960910";
    const char str2[] = "013";

    len = strcspn(str1, str2);

    printf("The first character matched in %d\n", len+1);
    return 0;
}

 

运行结果:

The first character matched in 11

posted @ 2019-12-17 15:36  王清河  阅读(271)  评论(0编辑  收藏  举报