码迷,mamicode.com
首页 > 其他好文 > 详细

strspn&strcspn

时间:2016-03-16 01:13:01      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

size_t strspn (const char *s,const char * accept);
strspn返回s中第一个不在accept中出现过的字符下标。
Returns an integer value specifying the length of the substring in str that consists entirely of characters in strCharSet.
If str begins with a character not in strCharSet, the function returns 0.
accept可以包含多个字符,从str开始搜索,计算str中包含的accept中任一字符的下标。
#include <string.h>
#include <stdio.h>
main()
{
    char *str="Linux was first developed for 386/486-based pcs.";
    printf("%d\n",strspn(str,"Linux"));
    printf("%d\n",strspn(str,"/-"));
    printf("%d\n",strspn(str,"1234567890"));
}

运行结果:

5

0

0

 
size_t strcspn(const char *s, const char * reject);
strcspn()从参数s 字符串的开头计算连续的字符, 而这些字符都完全不在参数reject 所指的字符串中.
简单地说, 若strcspn()返回的数值为n, 则代表字符串s 开头连续有n 个字符都不含字符串reject 内的字符.
返回字符串s 开头连续不含字符串reject 内的字符数目。
#include <string.h>
 
main(void)
 
{
 
    char *str = "Linux was first developed for 386/486-based pcs. ";
 
    printf("%d\n", strcspn(str, " "));
 
    printf("%d\n", strcspn(str, "/-"));
 
    printf("%d\n", strcspn(str, "1234567890"));
 
}
 

运行结果:

5

33

30

strspn&strcspn

标签:

原文地址:http://www.cnblogs.com/embedded-linux/p/5281740.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!