编程实现strcat

来源:互联网 发布:一个域名多少钱 编辑:程序博客网 时间:2024/06/15 23:28
库函数strcat把字符串内容连接到目标字符串的后面,所以应该从目标字符串的末尾也就是结束符"\0"的位置开始插入另一个字符串的内容

函数原型:char *strcat( char *strDestination, const char *strSource );

#include<stdio.h>#include<stdlib.h>char *MyStrcat( char *Des, const char *Src ){char *tmp;tmp=Des; //保存目的字符串首地址以便返回while(*Des++);Des--;//Des指向字符串结束符while(*Des++=*Src++);//进行循环复制*Des='\0';return tmp;}int main(){char *Des=(char *)malloc(256);*Des='\0';char *str1="Hello ";char *str2="World!";printf("%s\n",MyStrcat(MyStrcat(Des,str1),str2));free(Des);return 0;}

测试结果如下图: