strcspn函数

来源:互联网 发布:pdf 密码移除 mac 编辑:程序博客网 时间:2024/05/17 16:44
ohn,he like writing!"

    
int inttemp;

    inttemp
=strcspn(str1,str2);   //将str2中的每个字符均与str1中的匹配,如果这个字符在str1中出现过,则返回这个字符在str1中的索引值
     printf("The first index of the character both in str1 and str2: %d ", inttemp);
    
return 0;
}

在VC++ 6.0编译运行:

匹配过程是这样的:

str2中第一个字符“J”先与str1中的每个字符比较,发现没有相同的;

继续,str2中的“o”再与str1中的每个字符比较,发现仍然没有相同的;

继续,……

当继续到str2中的字符“k”时,在与str1中索引位置为5的字符比较时第一次发现相等,终止搜索,返回索引值5;

结束。

第二种情形(匹配失败):

#include <string.h>
#include 
<stdio.h>
int main()
...{
    
char *str1="aaaaakkkeeee";    //str1与str2中没有相同的字符
    char *str2="bbbcccddd"

    
int inttemp;

    inttemp
=strcspn(str1,str2);   //用strcspn函数
     printf("The first index of the character both in str1 and str2: %d ", inttemp);
    
return 0;
}

在VC++ 6.0编译运行:

由于str1与str2中没有相同的字符,匹配失败,返回字符串str1的长度12。

 


原创粉丝点击