当前位置: C语言 -- 标准库 -- <string.h> -- strspn

strspn函数


概要:
#include <string.h>
size_t strspn(const char *s1, const char *s2);

描述:

该函数计算参数s1指向字符串的最大初始片段长度,该片段由参数s2指向字符串中的字符组成。


参数:
const char *s1

指向字符串的指针。

const char *s2

指向字符串的指针。


返回值:

函数返回最大初始片段的长度。


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
/*函数strspn范例*/

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

int main(void)
{
    const char str[] = "You can fool all the people some of the time,\
                and some of the people all the time,\
                but you can not fool all the people all the time.";
    const char subStr[] = "aeiou";
    const char *ptr = str;
    size_t count = 0;
    size_t length;

    /*统计元音字母出现的次数。*/
    while(*ptr!='\0')
    {
        length = strspn(ptr, subStr);
        if(length==0)
            ++ptr;
        else
        {
            count += length;
            ptr += length;
        }
    }
    printf("Total %zu vowel%s.\n", count, (count>1)?"s":"");
    
    return 0;
}


输出:

Total 44 vowels.


相关内容:
memchr 搜索指定字符在内存区域第一次出现位置的函数。
strchr 搜索指定字符在字符串中第一次出现位置的函数。
strcspn 计算最大初始片段长度的函数。
strpbrk 搜索字符在字符串中第一次出现位置的函数。
strrchr 搜索指定字符在字符串中最后一次出现位置的函数。
strstr 搜索子字符串在字符串中第一次出现位置的函数。
strtok 拆分字符串的函数。