my_strcat函数实现
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <assert.h>
#include <string.h>
char* my_strcat(char* dest, const char* src)
{
assert(dest && src);
char* ret = dest;
//找目标串的'\0'
while (*dest != '\0')
{
dest++;
}
/*while (*src != '\0')
{
*dest = *src;
dest++;
src++;
}*/
//另一种写法
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr1[20] = "abc";
char arr2[] = "defgh";
char* pc = my_strcat(arr1, arr2);
printf("%s", pc);
return 0;
}
my_strcmp函数实现
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <assert.h>
#include <string.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;
}*/
//另一种写法
return (*str1 - *str2);
}
int main()
{
char arr1[] = "abcdf";
char arr2[] = "abcde";
int ret = my_strcmp(arr1, arr2);
printf("%d", ret);
return 0;
}
标签:return,dest,str2,str1,strcat,char,字符串,include,strcmp
From: https://blog.51cto.com/u_16003640/6165189