strspn实现方式之一

strspn

int ho_strspn(const char *s1, const char *s2) {
    int n = 0;
    const char *s; 
    for (; *s1; s1++, n++) {
        for (s = s2; *s && (*s1 != *s); s++) {
        }   
        if (*s == '\0')
            return n;
    }   

    return n;
}

strspn 的作用是返回s1中 第一个不在s2出现的字符的下标。

strspn 常见用法。

1.跳过空白字符

 char *p = " \t\r\nhello";

 printf("%s\n", p + ho_strspn(p, " \t\r\n"));


你可能感兴趣的:(strspn实现方式之一)