strcspn() — Find Offset of First Character Match

Format

#include <string.h>
size_t strcspn(const char *string1, const char *string2);

Language Level: ANSI

Threadsafe: Yes.

Locale Sensitive: The behavior of this function might be affected by the LC_CTYPE category of the current locale. For more information, see Understanding CCSIDs and Locales.

Description

The strcspn() function finds the first occurrence of a character in string1 that belongs to the set of characters that is specified by string2. Null characters are not considered in the search.

The strcspn() function operates on null-ended strings. The string arguments to the function should contain a null character (\0) marking the end of the string.

Return Value

The strcspn() function returns the index of the first character found. This value is equivalent to the length of the initial substring of string1 that consists entirely of characters not in string2.

Example that uses strcspn()

This example uses strcspn() to find the first occurrence of any of the characters "a", "x", "l", or "e" in string.

#include <stdio.h>
#include <string.h>
 
#define SIZE 40
 
int main(void)
{
  char string[SIZE] = "This is the source string";
  char * substring = "axle";
 
  printf( "The first %i characters in the string \"%s\" "
          "are not in the string \"%s\" \n",
            strcspn(string, substring), string, substring);
 
}
 
/**********  Output should be similar to:  **************
 
The first 10 characters in the string "This is the source string"
are not in the string "axle"
*/

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]