C语言strcspn()函数:检索字符串s1开头连续有几个字符都不含字符串s2中的字符

头文件:#inclued<string.h>

函数原型: int strcspn(char *str, char *strCharSet);

这个函数的的功能是,字符串str中第一次出现的某个字符,这个字符同时存在于 strCharSet中,返回这个字符在str中的索引值。
若字符串strCharSet 中,都没有一个字符和 str中的相同,则返回str的字符串长度。

注意:strcspn()会检查字符串结束标志'\0';如果str中的字符都没有在strCharSet中出现,那么将返回str的长度;检索的字符是区分大小写的。
范例:返回str,strCharSet包含的相同字符串的位置。

#include<stdio.h>

#include<string.h>

int main()

{

    char* s1 = "http://see.xidian.edu.cn/cpp/u/biaozhunku/";

    char* s2 = "c is good";

    int n = strcspn(s1,s2);

    printf("The first char both in s1 and s2 is :%c\n",s1[n]);

    printf("The position in s1 is: %d\n",n);

    return 0;

}

运行结果:

The first char both in s1 and s2 is :s

The position in s1 is: 7

再看一个例子,判断两个字符串的字符是否有重复的。

#include<stdio.h>

#include<string.h>

int main()

{

    char* s1 = "http://see.xidian.edu.cn/cpp/xitong/";

    char* s2 = "z -*";

    if(strlen(s1) == strcspn(s1,s2)){

        printf("s1 is diffrent from s2!\n");

    }else{

        printf("There is at least one same character in s1 and s2!\n");

    }

    return 0;

}

运行结果:

s1 is diffrent from s2!




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值