C语言标准函数库中包括 strcat 函数,用于字符串联接(复合加赋值)。作为练习,我们自己编写一个功能与之相同的函数。
函数原型
char* StrCat(char *dst, const char *src);
说明:src 为源串的起始地址,dst 为目的串起始地址。函数将 src 串添加到 dst 串末尾,函数值为 dst。
裁判程序
#include <stdio.h>
#include <string.h>
char* StrCat(char *dst, const char *src);
int main()
{
char a[1024], b[1024], c[1024];
gets(a);
gets(b);
gets(c);
StrCat(a, StrCat(b, c));
puts(a);
puts(b);
puts(c);
return 0;
}
/* 你提交的代码将被嵌在这里 */
输入样例
abc
de
f
输出样例
abcdef
def
f
程序1:
char* StrCat(char *dst, const char *src)
{
char* ret=dst;
while(*dst!='\0'){
dst++;
}
while(*src!='\0'){
*dst=*src;
dst++;
src++;
}
*dst='\0';
return ret;
}
程序2:
char* StrCat(char *dst, const char *src)
{
int i=0,j=0;
while(dst[i]!='\0'){
i++;
}
while(src[j]!='\0'){
dst[i]=src[j];
i++;
j++;
}
dst[i]='\0';
return dst;
}