模拟实现strcpy
#include<stdio.h>
#include<Windows.h>
#include<assert.h>
char *Mystrcpy(char *str1, const char *str2)//str2的值不能被更改所以用const修饰
{
char *ret = str1;
assert(str1 != NULL);
assert(str2 != NULL);
while ((*str1++ = *str2++)!='\0')
{
;
}
return ret;
}
int main()
{
char a[] = "abcdefg";
char b[50] = { 0 };
printf("%s\n", Mystrcpy(b, a));
system("pause");
return 0;
}
模拟实现strcat
#include<stdio.h>
#include<Windows.h>
#include<assert.h>
char *Mystrcat(char *str1, char *str2)
{
assert(str1 != NULL);
assert(str2 != NULL);
char *p = str1;
while (*str1 != '\0')
{
str1++;
}
while (*str1++ = *str2++)
{
;
}
return p;
}
int main()
{
char a[40] = "abcdefg";
char b[20] = "hijklmn";
printf("%s\n", Mystrcat(a, b));
system("pause");
return 0;
}