CPP
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;
//The strspn() function in C++ takes two string dest and src and gives the length of maximum initial segment of the string dest
//that consists of characters that are present in the string src.
size_t strspn(const char* s1, const char* s2)
{
const char *p1,*p2;
for(p1=s1; *p1; p1++)
{
for(p2=s2; *p2 ; p2++)
{
if ( *p2 == '\0' ) // end of s2 reached; no match found
{
return ( p1 - s1 ) ; // so *p1 is not in s2[]
}
else if ( *p1 == *p2 ) // *p1 is not the one
{
break ; // aborts inner for loop
}
}
}
return ( p1 - s1 ) ; // returning length of s1
}
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run