C - strspn() function
C strspn() function - Get length of a substring
Syntax:
size_t strspn(const char *str1, const char *str2)
The strspn() function is used to compute the length (in bytes) of the maximum initial segment of the string pointed to by str1 which consists entirely of bytes from the string pointed to by str2.
Parameters:
Name | Description | Required /Optional |
---|---|---|
str1 | Null-terminated string to search. | Required |
str2 | Null-terminated character set. | Required |
Return value from strspn()
- The strspn() function shall return the computed length.
- No return value is reserved to indicate an error.
Example: strspn() function.
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[] = "Programming";
char source[] = "Pogr";
int result;
result = strspn( string, source );
printf( "The first %d characters of \"%s\" are found in \"%s\"\n", result, string, source );
char source1[] = "Po";
result = strspn( string, source1 );
printf( "\nThe first %d characters of \"%s\" are found in \"%s\"\n", result, string, source1 );
char source2[] = "Por";
result = strspn( string, source2 );
printf( "\nThe first %d characters of \"%s\" are found in \"%s\"\n", result, string, source2 );
char source3[] = "Porm";
result = strspn( string, source3 );
printf( "\nThe first %d characters of \"%s\" are found in \"%s\"\n", result, string, source3 );
char source4[] = "orm";
result = strspn( string, source4 );
printf( "\nThe first %d characters of \"%s\" are found in \"%s\"\n", result, string, source4 );
}
Output:
The first 5 characters of "Programming" are found in "Pogr" The first 1 characters of "Programming" are found in "Po" The first 3 characters of "Programming" are found in "Por" The first 3 characters of "Programming" are found in "Porm" The first 0 characters of "Programming" are found in "orm"
C Programming Code Editor:
Previous C Programming: C strrchr()
Next C Programming: C strstr()
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/c-programming/string/c-strspn.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics