How to use and implement strspn in C

The strspn function scan the string pointed to by s1 and computes the length of the maximum initial segment of the string which consists entirely of characters from the string pointed to by s2. it means strspn returns the length of the initial segment of s1 which consists only of characters that are part of s2.

Syntax strspn in C:

//General syntax of strspn

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


Parameters:

s1— The null-terminated string to be scanned.

s2— The null-terminated string containing the characters to match.

Return:

The strspn function returns the length of the segment.

 

Let’s see an example code to understand the functionality of the strspn in C. In this C code, I am calculating the length of the string segment which has only characters that are present in string s1 and s2 both.

#include <stdio.h>
#include <string.h>

int main()
{
    int len;

    // initializing strings
    char s1[] = "Aticleworld";
    char s2[] = "Atic";

    // using strspn() to compute initial chars
    // before 1st unmatch chars.
    len = strspn(s1, s2);

    printf("Length of initial segment matching: %d\n", len);

    return 0;
}

When you run the program, the output will be:

strspn function

 

Important points you must know before using strspn in C:

1. You must include string.h header file before using the strspn function in C.

2. The behavior is undefined if either s1 or s2 is not a pointer to a null-terminated byte string.

3. If all of the characters in s1 are in s2, the function returns the length of the entire s1 string. Let’s see a c program.
#include <stdio.h>
#include <string.h>

int main()
{
    int len;

    // initializing strings
    char s1[] = "ABCDEF";
    char s2[] = "0A245BFC19ED";

    // using strspn() to compute initial chars
    // before 1st unmatch chars.
    len = strspn(s1, s2);

    printf("Length of initial segment matching: %d\n", len);

    return 0;
}

When you run the program, the output will be:

strspn in C

4. If the first character in s1 is not in s2, the function returns zero.
#include <stdio.h>
#include <string.h>

int main()
{
    int len;

    // initializing strings
    char s1[] = "ZBCDEF";
    char s2[] = "0A245BFC19ED";

    // using strspn() to compute initial chars
    // before 1st unmatch chars.
    len = strspn(s1, s2);

    printf("Length of initial segment matching: %d\n", len);

    return 0;
}

When you run the program, the output will be:

strspn when return zero

How to implement your own strspn function?

Your compiler/standard library will likely have a very efficient and tailored implementation of the strspn() function. So if not require avoid to create own version of the strspn function.

We can implement the strspn function in many ways. Here we are implementing strspn using the help of strchr function. The strchr function returns a pointer to the located character, or a null pointer if the character does not occur in the string. So let’s create our own version of the strspn() function in C.

Note: Below function only to understand the working of strspn. There is a lot of scenarios that are not handled in this function. You can handle the scenario as your requirement and if possible then use the standard strspn function (library function).

unsigned int my_strspn(const char *s1, const char *s2)
{
    unsigned int len =0;

    //return 0 if any one is NULL
    if((s1 == NULL) || (s2 == NULL))
        return len;

    //return s1 char position if found in s2
    //if not found return NULL
    while(*s1 && strchr(s2,*s1++))
    {
        len++;
    }
    return len;
}

 

Let’s create a small application to test our own strspn () function in C.

#include <stdio.h>
#include <string.h>

unsigned int my_strspn(const char *s1, const char *s2)
{
    unsigned int len =0;

    //return 0 if any one is NULL
    if((s1 == NULL) || (s2 == NULL))
        return len;

    while(*s1 && strchr(s2,*s1++))
    {
        len++;
    }
    return len;
}


int main()
{
    int len;

    // initializing strings
    char s1[] = "ABCDEF";
    char s2[] = "0A245BFC19ED";

    // using my_strspn() to compute initial chars
    // before 1st unmatch chars.
    len = my_strspn(s1, s2);

    printf("Length of initial segment matching: %d\n", len);

    return 0;
}

When you run the program, the output will be:

strspn in C

 

Recommended Articles for you: