C和指针之字符串简单实现strchr、strcmp函数

举报
chenyu 发表于 2021/07/26 23:08:39 2021/07/26
【摘要】 1、问题 简单实现strchr、strcmp函数       2、代码实现   #include <stdio.h>#include <string.h> /**简单模拟strchr函数**/char *my_strchr(const char *des, int ch){ if (des == N...

1、问题

简单实现strchr、strcmp函数

 

 

 

2、代码实现

 


  
  1. #include <stdio.h>
  2. #include <string.h>
  3. /**
  4. 简单模拟strchr函数
  5. **/
  6. char *my_strchr(const char *des, int ch)
  7. {
  8. if (des == NULL)
  9. return des;
  10. while (*des != '\0')
  11. {
  12. if (*des == ch)
  13. {
  14. return des;
  15. }
  16. ++des;
  17. }
  18. return NULL;
  19. }
  20. int main()
  21. {
  22. const char *des = "chenyu";
  23. char ch = 'y';
  24. printf("my_strchr is %s\n", my_strchr(des, ch));
  25. printf("strchr is %s\n", strchr(des, ch));
  26. return 0;
  27. }

 

 

 

 

 


  
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <assert.h>
  4. /**
  5. 简单实现strcmp函数
  6. **/
  7. int my_strcmp(const char *s1, const char *s2)
  8. {
  9. assert(s1 != NULL);
  10. assert(s2 != NULL);
  11. while (*s1 == *s2 && *s1 != '\0'

文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。

原文链接:chenyu.blog.csdn.net/article/details/78619211

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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