How to get the length of the first characters of str1 which include in str2 in C

3 Answers

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

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

// Returns the length of the initial portion of s1 which include only characters that are in s2

int main(void) 
{
    char s1[] = "123456F88";
    char s2[] = "87932844981";
   
    int i = strspn(s1, s2);
    printf ("First %d digits\n", i);
    
    return 0;
}
   
/*
run:

First 4 digits

*/

 



answered Feb 16, 2016 by avibootz
edited Aug 8, 2017 by avibootz
0 votes
#include <stdio.h>
#include <string.h> 

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

// Returns the length of the initial portion of s1 which include only characters that are in s2

int main(void) 
{
    char s1[] = "c c++";
    char s2[] = "java c#";
   
    int i = strspn(s1, s2); // c(space)c
    printf ("First %d characters\n", i);
    
    return 0;
}
   
/*
run:

First 3 digits 

*/

 



answered Aug 8, 2017 by avibootz
0 votes
#include <stdio.h>
#include <string.h> 

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

// Returns the length of the initial portion of s1 which include only characters that are in s2

int main(void) 
{
    char s1[] = "python c c++";
    char s2[] = "java c#";
   
    int i = strspn(s1, s2); // c(space)c is not the first part of s1
    printf ("First %d characters\n", i);
    
    return 0;
}
   
/*
run:

First 0 characters

*/

 



answered Aug 8, 2017 by avibootz
edited Aug 8, 2017 by avibootz
...