曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 4333|回复: 0
打印 上一主题 下一主题

[字符串] strspn() -- 返回字符串中连续包含指定字符的个数

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
跳转到指定楼层
楼主
发表于 2011-12-22 14:18:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-5-6 12:58 , Processed in 0.068732 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表