曲径通幽论坛

标题: strspn() -- 返回字符串中连续包含指定字符的个数 [打印本页]

作者: beyes    时间: 2011-12-22 14:18
标题: strspn() -- 返回字符串中连续包含指定字符的个数
strspn() 函数原型如下:
[Plain Text] 纯文本查看 复制代码
#include <string.h>
size_t strspn(const char *s, const char *accept);

该函数查看字符串 s 一共连续有几个字符是被包含在 accept 这个字符串中的,函数返回的是这些个字符的个数。

测试代码
[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <string.h>

int main()
{
        char *str = "hello linux world";

        printf("%d\n", strspn(str, "hello"));

        printf("%d\n", strspn(str, "zyxwvutsrqponmlkjhgfedcba"));        /* str 中有空格,accept 在检测时遇到空格停止*/


        printf("%d\n", strspn(str, "abcdefghjklmnopqrstuvwxyz "));      /* 这里的 accept 少了一个字符 'i' */

        printf("%d\n", strspn(str, "abcdefghijklmnopqrstuvwxyz "));     /* str 中的字符在 accept 里都能找得到*/



        return 0;
}

运行输出:
./strspn
5
5
7
17





欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2