C和指针之字符串简单实现strchr、strcmp函数
【摘要】 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、代码实现
-
#include <stdio.h>
-
#include <string.h>
-
-
/**
-
简单模拟strchr函数
-
**/
-
char *my_strchr(const char *des, int ch)
-
{
-
if (des == NULL)
-
return des;
-
while (*des != '\0')
-
{
-
if (*des == ch)
-
{
-
return des;
-
}
-
++des;
-
}
-
return NULL;
-
}
-
-
int main()
-
{
-
const char *des = "chenyu";
-
char ch = 'y';
-
printf("my_strchr is %s\n", my_strchr(des, ch));
-
printf("strchr is %s\n", strchr(des, ch));
-
return 0;
-
}
-
#include <stdio.h>
-
#include <string.h>
-
#include <assert.h>
-
-
/**
-
简单实现strcmp函数
-
**/
-
-
int my_strcmp(const char *s1, const char *s2)
-
{
-
assert(s1 != NULL);
-
assert(s2 != NULL);
-
while (*s1 == *s2 && *s1 != '\0'
文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。
原文链接:chenyu.blog.csdn.net/article/details/78619211
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)