C语言 编程 数组和指针

来源:百度知道 编辑:UC知道 时间:2024/09/24 23:44:43
编写一个函数,将字符串1复制到字符串2中。要求不使用strpy()函数

char* my_strcpy(char* s2, char* s1){
if(strlen(s1)>strlen(s2))
return ;/*串2比串1长,不能容纳,不复制,返回*/
while(*s1){
*s2=*s1;
s2++;
s1++;
}
*s2='\0';/*这句很重要,不然输出时和预期 的不同,因为字串没有结束符就会一直输出*/
}

char* str_cpy(char* str1, char* str2)
{
if(!str1 || !str2)
{
return str1;
}

while(0 != (*str1++ = *str2++));
return str1;
}

#include<stdio.h>
#include<string.h>

void str_copy(char *p,char *q)
{
int i,lenth1,lenth2;
lenth1=strlen(p);
lenth2=strlen(q);
for(i=lenth1; i<=lenth1+lenth2; i++)
{
p[i]=q[i-lenth1];
}
}

void main()
{
char a[20]={"123"},b[5]={"456"};
str_copy(a,b);
printf("%s",a);
}