strcspn函数

举报
悦来客栈的老板 发表于 2020/12/29 00:14:47 2020/12/29
【摘要】 /* strcspn 原型:size_t strcspn(const char *s1,const char *s2); 相关头文件:string.h 功能:顺序在字符串s1中搜寻与s2中字符的第一个相同字符,包括结束符NULL,返回这个字符在S1中第一次出现的位置。 说明:(返回字符串s1中第一个在s2中出现的字符在s1中的下标值,亦即在s1中出现而s2中没有出现的子串的长...

/*
strcspn
原型:size_t strcspn(const char *s1,const char *s2);
相关头文件:string.h
功能:顺序在字符串s1中搜寻与s2中字符的第一个相同字符,包括结束符NULL,返回这个字符在S1中第一次出现的位置。
说明:(返回字符串s1中第一个在s2中出现的字符在s1中的下标值,亦即在s1中出现而s2中没有出现的子串的长度。)

这个可以看做是strchr函数的字符串版,很明显得设立两个指针,同时来遍历s1和s2.

*/

 


  
  1. #include <stdio.h>
  2. size_t strcspn(const char *s1, const char *s2)
  3. {
  4. const char *p; //设立两个同类型的指针
  5. const char *r;
  6. size_t count = 0; //返回值
  7. for (p = s1; *p!= '\0'; p++)
  8. {
  9. for (r = s2; *r != '\0'; r++)
  10. {
  11. if (*r == *p)
  12. {
  13. return count;//找到字符则返回。
  14. }
  15. }
  16. ++count;//因为是返回这个字符在S1中第一次出现的位置,所以指针p每移动一次,count+1;
  17. }
  18. return count;//没有找到则返回空字符在S1中出现的位置
  19. }
  20. int main ()
  21. {
  22. char str[] = "fcba73";
  23. char keys[] = "12a4560890";
  24. int i;
  25. i = strcspn(str,keys);
  26. printf ("The first number in str is at position %d.\n",i+1);
  27. return 0;
  28. }


//linux源码


  
  1. size_t strcspn(const char *s, const char *reject)
  2. {
  3. const char *p;
  4. const char *r;
  5. size_t count = 0;
  6. for (p = s; *p != '\0'; ++p) {
  7. for (r = reject; *r != '\0'; ++r) {
  8. if (*p == *r)
  9. return count;
  10. }
  11. ++count;
  12. }
  13. return count;
  14. }


 

 


  
  1. **
  2. FUNCTION
  3. <<strcspn>>---count characters not in string
  4. INDEX
  5. strcspn
  6. ANSI_SYNOPSIS
  7. size_t strcspn(const char *<[s1]>, const char *<[s2]>);
  8. TRAD_SYNOPSIS
  9. size_t strcspn(<[s1]>, <[s2]>)
  10. char *<[s1]>;
  11. char *<[s2]>;
  12. DESCRIPTION
  13. This function computes the length of the initial part of
  14. the string pointed to by <[s1]> which consists entirely of
  15. characters <[NOT]> from the string pointed to by <[s2]>
  16. (excluding the terminating null character).
  17. RETURNS
  18. <<strcspn>> returns the length of the substring found.
  19. PORTABILITY
  20. <<strcspn>> is ANSI C.
  21. <<strcspn>> requires no supporting OS subroutines.
  22. */
  23. #include <string.h>
  24. size_t
  25. _DEFUN (strcspn, (s1, s2),
  26. _CONST char *s1 _AND
  27. _CONST char *s2)
  28. {
  29. _CONST char *s = s1;
  30. _CONST char *c;
  31. while (*s1)
  32. {
  33. for (c = s2; *c; c++)
  34. {
  35. if (*s1 == *c)
  36. break;
  37. }
  38. if (*c)
  39. break;
  40. s1++;
  41. }
  42. return s1 - s; //利用指针的特性,少了一个临时变量
  43. }


 

 

//这个程序太精妙了,很值得学习


  
  1. size_t strcspn( const char * str1, const char * str2 )
  2. {
  3. int count = 0;
  4. // map有32个字节的大小,也就是256个bit,可把map看成一个2维数组[32][8]
  5. unsigned char map[32] = {0};
  6. // 每个ASCII码(设为c)有8bit,把它分成2部分,低3位构成下标j(通过c&7(2进制为111)),
  7. // 高5位构成下标i(通过c>>3得到)。这样在map[i][j]中置1表示字符存在
  8. // 对于字符’1’,其ASCII码为0x31,右移3位得到6,和7与运算得到1,也就是在map[6]中的第一位.
  9. //一个unsigend char有8位,拆为高5位与低3位。前5位的范围有0~32,所以申请32大小的数组map。
  10. //直接将前5位映射成成数组下标,后3位范围0~7,正好是每一项char(8bit)。这个算法把时间与空间结合起来,效率非常高。
  11. while(*str2)
  12. {
  13. map[*str2 >> 3] |= (1 << (*str2 & 7));
  14. str2++;
  15. }
  16. map[0] |= 1;
  17. while(!(map[*str1 >> 3] & (1 << (*str1 & 7))))
  18. {//如果s1中没有
  19. count++;
  20. str1++;
  21. }
  22. return count;
  23. }


 

 

 

文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq523176585/article/details/11919647

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。