(standard c libraries translation )strspn

strspn, strcspn - search a string for a set of bytes
strspn,strcspn-在字符串中查找字节序列

所需头文件
#include <string.h>

size_t strspn(const char *s, const char *accept);
size_t strcspn(const char *s, const char *reject);

The strspn() function calculates the length (in bytes) of the initial segment of s which consists entirely of bytes in accept.
The strcspn() function calculates the length of the initial segment of s which consists entirely of bytes not in reject.
strspn函数计算从字符串s开始完全包含于accpet字符串的字符长度

strcspn函数计算从字符串s开始完全不包含与accept字符串的字符长度


testcase如下:

#include <stdio.h>
#include <string.h>

int main(void)
{
    const char *s1 = "123abcdefg";
    const char *s2 = "ab";
    const char *s3 = "12";

    int tmp1 = strcspn(s1, s2);
    int tmp2 = strspn(s1, s3);
    printf("tmp1 = %d\n", tmp1);
    printf("tmp2 = %d\n", tmp2);
    return 0;
}

运行结果如下:

cheny.le@cheny-ThinkPad-T420:~/cheny/testCode$ ./a.out
tmp1 = 3
tmp2 = 2

可以看到从“123abcdefg”开始完全不包含于“ab”字符串的字符是123三个,所以tmp1的值是3

从“123abcdefg”开始完全包含于“12”字符串的字符是12两个,所以tmp2的值是2


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值