#include <string.h>
size_t strcspn( const char *str1, const char *str2 );
函数返回 str1 开头连续 n 个字符都不含字符串 str2 内字符的字符数。即在 str1 中从左开始不包含 str2 中字符的字符个数。
查找字符串“1234567890”中,左边连续有几个字符不在“747DC8”字符串中。代码如下:
#include <stdio.h> #include <string.h> int main(void) { char *string1 = "1234567890"; char *string2 = "747DC8"; int length; length = strcspn(string1, string2); printf("Character where strings intersect is at position %d\n", length); return 0; }
输出结果:
Character where strings intersect is at position 3