Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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
gp-icon
Data structures & algorithms (Beginner to Intermediate)
Free guided path
13 chapters
99+ problems
gp-badge
Earn badges and level up

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) 
  • 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);
}

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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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!

Previous article
ispunct() in C
Next article
strcat() function in c
Guided path
Free
gridgp-icon
Data structures & algorithms (Beginner to Intermediate)
13 chapters
109+ Problems
gp-badge
Earn badges and level up
Live masterclass