#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
//方法一
void MyStrcpy1(char* s1, char* s2)
{
while (*s2 != '\0')
{
*s1++ = *s2++;
}
*s1 = *s2; //或者将该行改为: *s1 = '\0';
}
//方法二
void MyStrcpy2(char* s3, char* s4)
{
while (*s3++ = *s4++);
}
//方法三
char* MyStrcpy3(char* s5, const char* s6)
{
char* ret = s5; //存储首元素地址
while (*s5++ = *s6++);
return ret; //返回解引用后的首元素地址
}
int main()
{
char str1[] = "************";
char str2[] = "welcome";
char str3[] = "************";
char str4[] = "thank you";
char str5[] = "************";
char str6[] = "hello";
MyStrcpy1(str1, str2);
MyStrcpy2(str3, str4);
printf("%s\n",str1);
printf("%s\n",str3);
printf("%s\n", MyStrcpy3(str5, str6));
}
c语言-复制字符串3种方法(类似strcpy()函数)
于 2022-07-24 18:56:27 首次发布