C String Functions - C strspn






Finds the first sequence of characters in the string str1 that contains any character specified in str2.

Returns the length of this first sequence of characters found that match with str2.

Prototype

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

Parameter

This function has the following parameter.

str1
C string to be scanned.
str2
C string containing the characters to match.

Return

The length of the initial portion of str1 containing only characters that appear in str2.

If all of the characters in str1 are in str2, the function returns the length of the entire str1 string.

If the first character in str1 is not in str2, the function returns zero.

size_t is an unsigned integral type.





Example


#include <stdio.h>
#include <string.h>
/*from  ww w . j a  v  a2s.  c  o  m*/
int main (){
  int i;
  char strtext[] = "this is a test 123321";
  char cset[] = "1234567890";

  i = strspn (strtext,cset);
  printf ("The initial number has %d digits.\n",i);
  return 0;
} 

       

The code above generates the following result.





Example 2


#include<string.h> 
#include<stdio.h> 
//w  w  w . ja v a  2s . c o m
int main(void) { 
     char string[]="7803 Main St."; 

     printf("The number length is %d.\n",strspn (string,"1234567890")); 

     return 0; 
} 

The code above generates the following result.

Example 3


#include <string.h>
#include <stdio.h>
 /*from  w  w  w  . j  a  v  a2s  .  c om*/
int main(void){
    const char *string = "abcde312$#@";
    const char *low_alpha = "qwertyuiopasdfghjklzxcvbnm";
 
    size_t spnsz = strspn(string, low_alpha);
    printf("skipping lowercase from '%s'\n"
           "The remainder is '%s'\n", string, string+spnsz);
}

The code above generates the following result.