# C `strspn()`函数

> 原文： [https://beginnersbook.com/2017/11/c-strspn-function/](https://beginnersbook.com/2017/11/c-strspn-function/)

**函数`strspn()`**在给定字符串中搜索指定的字符串，并返回给定字符串中匹配的字符数。

## C `strspn()`声明

```c
size_t strspn(const char *str1, const char *str2)
```

`str1` - 搜索字符串`str2`的字符的字符串。

`str2` - 另一个字符串，在`str1`中搜索该字符串的字符。

## `strspn()`的返回值

它返回给定字符串中匹配的字符数。

## 示例：`strspn()`函数

```c
#include <stdio.h>
#include <string.h>
int main () {
   int len; 
   const char str1[] = "abcdefgh"; 
   const char str2[] = "abXXcdeZZh";
   /* Searching the string str2 in the string str1.
    * It returns the count of characters of str2 that
    * are matched in the str1 
    */
   len = strspn(str1, str2);
   printf("Number of matched characters: %d\n", len );
   return 0;
}
```

输出：

```c
Number of matched characters: 5
```

#### 相关文章：

1.  [C - `strrchr()`函数](https://beginnersbook.com/2017/11/c-strrchr-function/)
2.  [C - `strncpy()`函数](https://beginnersbook.com/2017/11/c-strncpy-function/)
3.  [C - `strcoll()`函数](https://beginnersbook.com/2017/11/c-strcoll-function/)
4.  [C - `strcmp()`函数](https://beginnersbook.com/2017/11/c-strcmp-function/)