strcspn()

Find the first character of one string that is in another string.

  #include <string.h>
  
  size_t strcspn(const char *s1, const char *s2);    
Parameter

s1

The string to search.

s2

A string containing a list of characters to search for.

Remarks

This function finds the first occurrence in the string pointed to by s1 of any character in the string pointed to by s2. These strings must be null-terminated. The function starts examining characters at the beginning of s1 and continues searching until a character in s1 matches a character in s2.

The function returns the index of the first character in s1 that matches a character in s2. The function considers the null characters that terminate the strings.

This facility may not be available on some configurations of the EWL.

Listing: Example of strcspn() usage

#include <string.h>

#include <stdio.h>

int main(void)

{

char s1[] = "chocolate *cinnamon* 2 ginger";

char s2[] = "1234*";

size_t i;

printf(" s1 = %s\n s2 = %s\n", s1, s2);

i = strcspn(s1, s2);

printf("Index returned by strcspn() is %d.\n", i);

printf("Indexed character is %c.\n", s1[i]);

return 0;

}

Output:

s1 = chocolate *cinnamon* 2 ginger

s2 = 1234*

Index returned by strcspn() is 10.

Indexed character is *.