函数 strpbrkstrcasecmp strspn

mac2022-06-30  29

strpbrk函数

  函数原型:extern char *strpbrk(char *str1, char *str2)

  参数说明:str1待比较的字符串,str2为指定被搜索的字符串。          所在库名:#include <string.h>    函数功能:比较字符串str1和str2中是否有相同的字符,如果有,则返回该字符在str1中的位置的指针。    返回说明:返回指针,搜索到的字符在str1中的索引位置的指针。

  例子:

  #include<string.h>  #include<stdlib.h>  #include <stdio.h>

  int main(int argc, char *argv[])  {     char *a = "aBcDeF";     char *b = "AbCdEf";

    char* c = strpbrk(a, "c");     printf("%s\n", c);

    return 0;  }

  执行 cDeF

 

 

strcasecmp(忽略大小写比较字符串)

  函数原型: int strcasecmp (const char *s1, const char *s2);

  所在库名:#include <string.h>(不是C/C++的标准头文件,区别于string.h[1] )

  函数功能:用来比较参数s1和s2字符串,比较时会自动忽略大小写的差异。

  返回说明:若参数s1和s2字符串相等则返回0。s1大于s2则返回大于0 的值,s1 小于s2 则返回小于0的值。

  例子:

  #include <strings.h>   main()   {     char *a="aBcDeF";     char *b="AbCdEf";     if(!strcasecmp(a,b))     printf("%s=%s\n",a,b);   }   执行 aBcDeF=AbCdEf   注释:该函数是二进制安全的,且对大小写不敏感。此函数只在Linux中提供,相当于windows平台的 stricmp。     strspn(返回字符串中第一个不在指定字符串中出现的字符下标)

  函数原型:size_t strspn (const char *s,const char * accept);

  所在库名:#include<string.h>

  函数功能:从参数s 字符串的开头计算连续的字符,而这些字符都完全是accept 所指字

    0 符串中的字符。简单的说,若strspn()返回的数值为n,则代表字符串s 开头连续有n 个字符都是属于字符串accept内的字符。

  返回说明:返回字符串s开头连续包含字符串accept内的字符数目。

  例子:

  #include <strings.h>   main()   {

    char* str = "Linux was first developed for 386/486-based pcs";    printf("%d\n", strspn(str, "Linux"));    printf("%d\n", strspn(str, "/-"));    printf("%d\n", strspn(str, "1234567890"));

  }   执行:      5     0

转载于:https://www.cnblogs.com/AngelAI/p/3873714.html

最新回复(0)