c语言函数strspn()如何返回从字符串开头连续包含特定字符的字符数目实例源码介绍



c语言函数strspn()如何返回从字符串开头连续包含特定字符的字符数目实例源码介绍:

需要引入的头文件:#include <string.h>

strspn()方法的定义:size_t strspn(const char *s, const char * accept);

strspn()方法使用说明:strspn()从参数s 字符串的开头计算连续的字符,而这些字符都完全是accept 所指字符串中的字符。简单的说,若strspn()返回的数值为n,则代表字符串s 开头连续有n 个字符都是属于字符串accept 内的字符。

strspn()函数返回值:返回字符串s 开头连续包含字符串accept 内的字符数目。

strspn()函数使用实例源码介绍:
#include <string.h>
main(){
char *str = “Linux was first developed for 386/486-based PCs. “;
char *t1 = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”;
printf(“%d\n”, strspn(str, t1));
}

执行结果:
5 //计算大小写字母. 不包含” “, 所以返回Linux 的长度.