strspn() - String Manipulation Function

How to get the length of substring in C++?

Explanation

strspn() manipulation function is used to get the length of characters of str1 which consists only of characters contained in str2. If all the characters of str1 are in str2, then the result would be of str1, otherwise returns zero.

Syntax:


size_t strspn ( const char * str1, const char * str2 );

Example :



#include <stdio.h>
#include <cstring.h>
int main ()
{
int j;
char str1[] = "abcde";
char str2[] = "abecfggwhfh";
j = strspn (str1,str2);
printf ("The length of initial
substring of str1 is:: %d.\n",j);
return 0;
}

Result :

The length of initial substring of str1 is:: 3

In the above example, strspn() manipulation function is used to check the initial substring "abcde" in str2 to get the result. Even though 4 characters out of the 5 characters of str1 are in str2, the result gives three as the character "d" is not in the str2.

C++ Tutorial


Ask Questions

Ask Question