The strcspn () function in C

The function size_t strcspn (const char * str1, const char * str2) calculates the length of the character segment of string str1 without containing the characters in str2.

Declare the strcspn () function in C

Here is the declaration for strcspn () in C:

 size_t strcspn ( const char * str1 , const char * str2 ) 

Parameters

str1 - This is a string of characters to be scanned.

str2 - String contains a list of characters to match in str1.

Returns the value

This function returns the number of characters in a string segment of the original string str1 that is not in the str2 string.

For example

The following C program illustrates the usage of strcspn () in C:

 #include #include int main () { int len ; const char str1 [] = "ABCDEF4960910" ; const char str2 [] = "013" ; len = strcspn ( str1 , str2 ); printf ( "Ky tu ket noi dau tien tai vi tri %dn" , len + 1 ); return ( 0 ); } 

Compiling and running the above C program will result:

The strcspn () function in C Picture 1

According to Tutorialspoint

Previous article: Function strncpy () in C

Next lesson: strerror () function in C

5 ★ | 1 Vote

May be interested

  • The strerror () function in CPhoto of The strerror () function in C
    the function char * strerror (int errnum) searches for an internal array for errnum error numbers and returns a pointer to a string of error messages.
  • The strlen () function in CPhoto of The strlen () function in C
    the function size_t strlen (const char * str) calculates the length of str string (not including the ending null character).
  • The function strpbrk () in CPhoto of The function strpbrk () in C
    char * strpbrk (const char * str1, const char * str2) finds the first character in str1 string that connects any character specified in str2. (not including null ending characters).
  • The strrchr () function in CPhoto of The strrchr () function in C
    char * strrchr (const char * str, int c) looks for the last occurrence of the character c (an unsigned char) in the string pointed to by parameter str.
  • The strspn () function in CPhoto of The strspn () function in C
    function size_t strspn (const char * str1, const char * str2) calculates the length of the first character segment of the str1 string containing the characters in str2
  • The strtok () function in CPhoto of The strtok () function in C
    char * strtok function (char * str, const char * delim) divides the string str into a sequence of tokens separately separated by delim separators (eg comma, ...).