strspn() — Search string

Standards

Standards / Extensions C or C++ Dependencies

Language Environment
ISO C
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3

both  

Format

#include <string.h>

size_t strspn(const char *string1, const char *string2);

General description

Calculates the length of the maximum initial portion of the string pointed to by string1 that consists entirely of the characters contained in the string pointed to by string2.

Returned value

strspn() returns the length of the substring found.

Example

CELEBS50
/* CELEBS50                                      

   This example finds the first occurrence in the array string                  
   of a character that is neither an a, b, nor c. Because the                   
   string in this example is cabbage, &strspn. returns 5, the                   
   length of the segment of cabbage before a character that is                  
   not an a, b or c.                                                            
                                                                                
 */                                                                             
#include <stdio.h>                                                              
#include <string.h>                                                             
                                                                                
int main(void)                                                                  
{                                                                               
  char * string = "cabbage";                                                    
  char * source = "abc";                                                        
  int index;                                                                    
                                                                                
  index = strspn( string, "abc" );                                              
  printf( "The first %d characters of \"%s\" are found in \"%s\"\n",            
              index, string, source );                                          
}                                                                               
                                                                                
Output
The first 5 characters of "cabbage" are found in "abc"