1 strcat函数实现
#include <stdio.h>
//简单实现strcat函数
char *my_strcat(char *des, const char *src)
{if (des == NULL || src == NULL)return des;char *result = des;//把指针移到末尾while (*des)des++;printf("*des is %c\n", *des);while ((*des++ = *src++) != '\0');return result;
}
int main()
{char des[30] = "chenyu";const char *src = "hello";printf("des is %s and my_strcat result is %s\n",des, my_strcat(des, src));return 0;
}
运行结果:
/**
vim my_strcat.c
gcc -g my_strcat.c -o strcat
./strcat
*des is
des is chenyuhello and my_strcat result is chenyuhello
**/
2 strchr函数实现
#include <stdio.h>
#include <string.h>/**
简单模拟strchr函数
**/
char *my_strchr(const char *des, int ch)
{if (des == NULL)