strcat:字符串追加
模拟实现strcat
#include <stdio.h>
#include <assert.h>
char* my_strcat(char* dest, const char* src)
{
assert(dest);
assert(src);
char* ret = dest;//记录起始地址
//1. 找到目标空间的末尾,即'\0'
while (*dest != '\0')
{
dest++;
}
//2. 拷贝字符串
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr1[20] = "hello ";
my_strcat(arr1,"world");
printf("%s\n", arr1);
return 0;
}
strcmp:比较两个字符串是否相等
模拟实现strcmp
#include <stdio.h>
#include <assert.h>
int my_strcmp(const char* str1,const char* str2)
{
assert(str1 && str2);
while (*str1 == *str2)
{
if (*str1 == '\0')
return 0;//相等
str1++;
str2++;
}
if (*str1 > *str2)
return 1;
else
return -1;
}
int main()
{
char arr1[] = "abc";
char arr2[] = "ac";
int ret = my_strcmp(arr1, arr2);
if (ret < 0)
{
printf("<\n");
}
else if (ret == 0)
{
printf("==\n");
}
else
{
printf(">\n");
}
return 0;
}
标签:return,dest,str2,str1,strcat,char,strcmp,库函数
From: https://blog.csdn.net/2301_80247375/article/details/145232129