Jump to: navigation, search

Description

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

Return value

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

Prototype

Declared in string.h

The C89/C99 prototype is:

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

Implementation

In standard C, this can be implemented as:

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

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

References

Personal tools