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:
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.
#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:
#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:
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:
Recommended Articles for you:
- Best Gifts for the programmer and techies.
- How to use and implement your own strcat in C.
- Implement and use of strpbrk in C.
- Implement and use of memset of in C
- Use and create strcspn in programming.
- How to make memcpy function in C
- Implement own memmove in C.
- memmove vs memcpy.
- Implement vector in C.
- How to Use strncpy() and implement own strncpy().
- How to pass an array as a parameter?
- Implement own atoi in C.
- 10 Best C Programming Books.
- Best mouse for a programmer.
- Dangling, Void, Null, and Wild Pointers
- Memory Layout in C.
- File handling in C, In a few hours.