Share this page

# Example: strspn

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

Tips to improve your search results... VuGen Function Reference Version 25.1-25.3 Function Reference 25.1-25.3 All View Local Help Center View Online Help Center Version: