strspn

字符含义

编辑函数

strspn(返回字符串中第一个不在指定字符串中出现的字符下标)spa

表头文件code

1htm

#include <string.h>ci

定义函数:字符串

1get

size_t strspn (const char *s,const char * accept);原型

函数说明 strspn()从参数s 字符串的开头计算连续的字符,而这些字符都彻底是accept 所指字符串中的字符。简单的说,若strspn()返回的数值为n,则表明字符串s 开头连续有n 个字符都是属于字符串accept内的字符。string

返回值 返回字符串s开头连续包含字符串accept内的字符数目。io

举例

编辑

1

2

3

4

5

6

7

8

9

#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

 

函数原型

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

int strspn(const char *s,const char *accept)

{

    const char *p;

    const char *a;

    int count = 0;

    for(p = s; *p != '\0'; ++p)

    {

        for (a = accept; *a != '\0'; ++a)

        {

            if (*p == *a)

            {

                                ++count;

                break;

            }

        }//里面的

for循环到此为止

        if (*a == '\0')

        {

            return count;

        }

        //++count;

    }//外面的for循环到此为止

    return count;

}

相关文章
相关标签/搜索