头文件:#inclued<string.h>
strcspn() 用来计算字符串 str 中连续有几个字符都不属于字符串 accept,其原型为:
int strcspn(char *str, char *accept);
【参数说明】str、accept为要进行查找的两个字符串。
strcspn() 从字符串 str 的开头计算连续的字符,而这些字符都完全不在字符串 accept 中。简单地说,若 strcspn() 返回的数值为 n,则代表字符串 str 开头连续有 n 个字符都不含字符串 accept 中的字符。
【返回值】返回字符串 str 开头连续不含字符串 accept 内的字符数目。
注意:如果 str 中的字符都没有在 accept 中出现,那么将返回 atr 的长度;检索的字符是区分大小写的。
提示:函数 strspn() 的含义与 strcspn() 相反,可以对比学习。
【示例】返回s1、s2包含的相同字符串的位置。
- #include<stdio.h>
- #include <stdlib.h>
- #include<string.h>
- int main()
- {
- char* s1 = "http://www.cdsy.xyz/computer/programme/C_language/20201230/cd16092637316818.html";
- char* s2 = "c is good";
- int n = strcspn(s1,s2);
- printf("The first char both in s1 and s2 is :%c\n",s1[n]);
- printf("The position in s1 is: %d\n",n);
- system("pause");
- return 0;
- }
运行结果:
再看一个例子,判断两个字符串的字符是否有重复的。
- #include<stdio.h>
- #include <stdlib.h>
- #include<string.h>
- int main()
- {
- char* s1 = "http://www.cdsy.xyz/";
- char* s2 = "z -+*";
- if(strlen(s1) == strcspn(s1,s2)){
- printf("s1 is diffrent from s2!\n");
- }else{
- printf("There is at least one same character in s1 and s2!\n");
- }
- system("pause");
- return 0;
- }
运行结果:s1 is diffrent from s2!