Use C strcspn function to find the index of a sub string

Syntax

C strcspn function has the following format.

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

C strcspn function is from header file string.h.

Description

C strcspn function returns the index of the first character in *str1 that matches any of the characters in *str2.

Example

Use C strcspn function to find the index of a sub string.


#include <string.h>
#include <stdio.h>
/*from  w w  w.  ja v  a 2  s  . c  o  m*/
int main(void)
{
  int len;
  len = strcspn("this is a test", "ab");
  printf("%d", len);

  return 0;
}

The code above generates the following result.





















Home »
  C Language »
    Function Reference »




assert.h
ctype.h
math.h
setjmp.h
signal.h
stdio.h
stdlib.h
string.h
time.h
wctype.h