C语言 strspn()函数

来源:这里教程网 时间:2026-02-16 12:54:01 作者:

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

C strspn()声明

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

str1 – 搜索字符串str2的字符的字符串。

str2 – 另一个字符串,在str1中搜索该字符串的字符。

strspn()的返回值

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

示例:strspn()函数

#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;}

输出:

Number of matched characters: 5

相关文章:

    C – strrchr()函数C – strncpy()函数C – strcoll()函数C – strcmp()函数

相关推荐