编写连接两个字符串函数strcat和字符串复制函数strcpy。

2024-11-20 23:01:34
推荐回答(3个)
回答1:

#include
void strcpy(char *s1, char *s2)
{
if(s1==NULL || s2==NULL) return;
while(*s2 != '\0') *s1 ++ = *s2++;
*s1 = '\0';
}
void strcat(char *s1, char *s2)
{
if(s1==NULL || s2==NULL) return;
while(*s1 != '\0') s1 ++;
while(*s2 != '\0') *s1 ++ = *s2++;
*s1 = '\0';
}
int main(void)
{
char s1[100] = "abcd";
char s2[100] = "1234";
char s3[100] = "UVWXYZ";
strcat(s1, s2);
strcpy(s2, s3);
printf("%s\n", s1);
printf("%s\n", s2);
return 0;
}

回答2:

char *strcat(char *source,char *destin)
{
while(*source);
do{
*source++=*destin;
}while(*destin++);
}

char *strcpy(char *str1,char *str2)
{
char *p,*q;
if(str1==null and str2==null)return null;
p=str1;q=str2;
while(*p!='\0' &&*q!='\0'){*p++=*q++;}
return str1;
}

回答3:

百度百科中有