Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

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
...