Jump to: navigation, search

Description

The strspn() function computes the length of the maximum initial segment of the string pointed to by s1 which consists entirely of characters from the string pointed to by s2.

Return value

The strspn() function returns the length of the segment.

Prototype

Declared in string.h

The C89/C99 prototype is:

size_t strspn(const char *s1, const char *s2);

Implementation

In standard C, this can be implemented as:

#include <string.h> /* size_t strchr() */
size_t strspn(const char *s1, const char *s2)
{
    size_t ret=0;
    while(*s1 && strchr(s2,*s1++))
        ret++;
    return ret;    
}

Compilable unit, portable C90 in implementation namespace; public domain; past reviewers: none; current reviews: none

References

The C Standard, 7.21.5.6 (C99 numbering)

Personal tools