The strcspn 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 not from the string pointed to by s2. It means strcspn scan s1 until the first occurrence of any characters that are part of s2.
Syntax strcspn in C:
//General syntax of strcspn size_t strcspn(const char *s1, const char *s2);
Parameters:
s1— The string to be scanned.
s2— The string containing the characters to match.
Return:
The strcspn function returns the length of the segment.
Let’s see an example code to understand the functionality of the strcspn in C. In this C code, I am calculating the length of the string segment which has characters that are not present in string s1 and s2 both.
#include <stdio.h> #include <string.h> int main() { int len; // initializing strings char s1[] = "Aticleworld"; char s2[] = "2e"; // using strcspn() to compute initial chars // before 1st matching chars. // returns 3 len = strcspn(s1, s2); printf("length of string that characters not present in s2: %d\n", len); return 0; }
When you run the program, the output will be:
Important points you must know before using strcspn in C:
1. You must include string.h header file before using the strcspn function in C.
2. The behavior is undefined if either s1 or s2 is not a pointer to a null-terminated byte string.
3. The strcspn function returns the length of string s1 if no characters of s1 matched to s2. Let’s see a c program.
#include <stdio.h> #include <string.h> int main() { int len; // initializing strings char s1[] = "Aticleworld"; char s2[] = "bzk"; // using strcspn() to compute initial chars // before 1st matching chars. // returns 3 len = strcspn(s1, s2); printf("length of string that characters not present in s2: %d\n", len); return 0; }
When you run the program, the output will be:
How to write your own strcspn in C?
Your compiler/standard library will likely have a very efficient and tailored implementation of the strcspn() function. So if not require avoid to create own version of the strcspn function.
We can implement the strcspn function in many ways. Here we are implementing strcspn 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 strcspn() function in C.
Note: Below function only to understand the working of strcspn. 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 strcspn function (library function).
unsigned int my_strcspn(const char *s1, const char *s2) { unsigned int len =0; //return 0 if any one is NULL if((s1 == NULL) || (s2 == NULL)) return len; //till not get null character while(*s1) { //return s1 char position if found in s2 if(strchr(s2,*s1)) { return len; } else { //increment s1 s1++; //increment len variable len++; } } return len; }
Let’s create a small application to test our own strcspn () function in C.
#include <stdio.h> #include <string.h> unsigned int my_strcspn(const char *s1, const char *s2) { unsigned int len =0; //return 0 if any one is NULL if((s1 == NULL) || (s2 == NULL)) return len; //till not get null character while(*s1) { //return s1 char position if found in s2 if(strchr(s2,*s1)) { return len; } else { //increment s1 s1++; //increment len variable len++; } } return len; } int main() { int len; // initializing strings char s1[] = "Aticleworld"; char s2[] = "bzk"; // using strcspn() to compute initial chars // before 1st matching chars. // returns 3 len = my_strcspn(s1, s2); printf("length of string that characters not present in s2: %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 memset of in C
- Use and create strspn 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.