编程实现strcpy函数

函数接口为char *mystrcpy(char *str1,char *str2)

要求:把str2指向的字符串拷贝到str1中去,并返回字符串str1的指针。

#include
using namespace std;
char *mystrcpy(char *str1, char *str2)
{
char *p = str1;
if (p == NULL || str2 == NULL)
{
cout << "the string is error!" << endl;
exit(0);
}
while (*str2 != '\0')
{
*p = *str2;
p++;
str2++;
}
*p = '\0';
return str1;
}
int main()
{
char a[10] = "hello";
char b[6] = "world";
char *ret = mystrcpy(a, b);
cout << ret;
getchar();
return 0;
}

你可能感兴趣的:(C++,字符串)