Table of contents
1.
Introduction
2.
strspn()
2.1.
Syntax
2.2.
Code
2.3.
Output
3.
Frequently Asked Questions
3.1.
What does strspn() do in C?
3.2.
What does strspn() return?
3.3.
How do we import strspn() into our code in C?
3.4.
Why do we use string functions like strspn() in C?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

strspn() in C

Author SAURABH ANAND
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

C is a procedure-oriented programming language created by Dennis Ritchie in 1972. C language provides us with multiple string functions that can save us from writing many lines of code. We can use the default functions instead of the long codes to get our work done. The string functions are included in the "string.h"header file. 

In this article, let’s learn about a string function - strspn().

strspn()

The strspn() is a C function that returns the length of the starting substring of the string referenced to by string1 that contains just the characters from the string pointed to by string2. Let’s learn how to use strspn() with an example.

Syntax

Following is the declaration for strspn() function.

size_t strspn(const char *string1, const char *string2) 
You can also try this code with Online C Compiler
Run Code
  • string1 − This is the string to be scanned.
  • string2 − This is the string containing the list of characters to match in string1.

Also read about “strcat() function in C, and  Tribonacci Series

Code

The following code shows the uses of the strspn() function. We will take two strings, "string1" and "string2," then we will return the length of the starting substring of the string referenced to by string1 that contains just the characters from the string pointed to by string2.

#include <stdio.h>
#include <string.h>
  
int main () {
char string1[] = "codingninjas";
   char string2[] = "code";
   int len = strspn(string1,string2);
    printf("Length of matching string is : %d\n", len );       
   return(0);
}
You can also try this code with Online C Compiler
Run Code

Output

Length of initial segment matching : 3

 

Note: We initialized the two strings in the above code, but we can also allow the users to give any input of their choice and execute this function on them. 

You can also read about the dynamic arrays in c, Short int in C Programming and C Static Function 

Must Read Decision Making in C

Frequently Asked Questions

What does strspn() do in C?

The strspn() function returns the length of the starting substring of the string referenced to by string1 that contains just the characters from the string pointed to by string2. 

What does strspn() return?

The strspn() function returns the length(integer). 

How do we import strspn() into our code in C?

We import the libraries by including header files in the C programming language. We include the string.h header file in the code to use the strspn() string function.  

Why do we use string functions like strspn() in C?

The string functions in C are easy to use, and they can replace lengthy codes with a single function. Long lines of code might increase the complexity of our code, and it can take a lot of time to execute. So we use string functions to overcome this problem.

Conclusion

In this article, we have extensively discussed the concept of the strspn() function in C, along with the steps and code.

Check out this problem - Multiply Strings

We hope this blog has helped you enhance your knowledge regarding the strspn() function and if you would like to learn more, check out our article on introduction to Cstring.hstring compare in C, and strpbrk(). Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass