How to use strspn() to get the length of the initial substring of s1 which consists entirely s2 in C

2 Answers

0 votes
#include <stdio.h>
#include <string.h>

int main()
{
	const char s1[] = "abcdef0123";
	const char s2[] = "abc";

	printf("%d\n", (int)strspn(s1, s2));
	printf("%d\n", (int)strspn(s1, "abe"));
	
    return 0;
}
         
           
           
           
/*
run:
           
3
2
        
*/

 



answered Jul 13, 2020 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
 
int main()
{
    const char s1[] = "31FC";
    const char s2[] = "0123456789abcdefABCDEF";
 
    printf("%d\n", (int)strspn(s1, s2));
    printf("%d\n", (int)strspn(s1, "38"));
    printf("%d\n", (int)strspn(s1, "1FC"));
    printf("%d\n", (int)strspn(s1, "13"));
    printf("%d\n", (int)strspn(s1, "CF31"));
    printf("%d\n", (int)strspn(s1, "CF31111333"));
    printf("%d\n", (int)strspn(s1, "CF31X"));
    printf("%d\n", (int)strspn(s1, "0123xyCCCCCQAZFGH"));
     
    return 0;
}
          
            
            
            
/*
run:
            
4
1
0
2
4
4
4
4
         
*/

 



answered Jul 13, 2020 by avibootz
edited Jul 13, 2020 by avibootz
...