strspn

String Manipulation Functions

Returns the length of the leading characters in a string that are contained in a specified string.

size_t *strspn( const char *string, const char *skipset);

string Null-terminated string to be scanned.
skipset Null-terminated string containing character set.

strspn returns the length of the substring that starts at the beginning of string and contains only characters that are in skipset.

Return Values

Returns the length of the initial substring of argument string that is only composed of characters included in skipset.

Example

The following example uses strspn to find the length of the first substring containing the characters o and r found within the word "corroborative" .

However, the first invocation of strspn fails because str starts with the letter c. The string must begin with one of the characters of the set. By incrementing the pointer of str one character forward (str+1) to the letter o, the next invocation of strspn succeeds, finding the first 4 characters to be either an o or an r.

    #include <string.h>
    char * str = "corroborative";
    int rc;
    if ((rc = strspn(str, "ro")) == 0)
        lr_output_message ("No o's or r's found");
    else
        lr_output_message ("%d of %d characters are an o or an r", rc, strlen(str));
    if ((rc = strspn(str + 1, "ro")) == 0)
        lr_output_message ("No o's or r's found");
    else
        lr_output_message ("%d of %d characters are an o or an r", rc, strlen(str));
Example: Output:
Action.c(7): No o's or r's found
Action.c(14): 4 of 13 characters are an o or an r