用指针实现两个字符串首尾连续的函数strcat(s,t)

来源:百度知道 编辑:UC知道 时间:2024/06/14 02:56:33
具体程序??
是用C语言,还要用到指针,要已经调试好的程序

一楼孤单拖鞋的实现有问题,可能会导致内存越界错误。
安全的做法要用动态分配才行,不过传入的字符串必须也是动态分配的。
strcat(char *s,char *t){
char *p = s;
s = malloc( strlen(s) + strlen(t) );
while(!(*p))
*(s++)=*(p++);
while(!(*t))
*(s++)=*(t++);
*s=0;
}

C的话
strcat(char *s,char *t){
while(!*(s++));
while(!*(t))
*(s++)=*(t++);
*s=0;
}
把t接到s后

MSDN上就有,给你一个吧,自己拿去改改吧
#include <string.h>
#include <stdio.h>

int main( void )
{
char string[80];
char* s1="Hello world from ";
char* s2="strcpy " ;
// Note that if you change the previous line to
// char string[20];
// strcpy and strcat will happily overrun the string
// buffer. See the examples for strncpy and strncat
// for safer string handling.

strcpy( string, s1 ); // C4996
// Note: strcpy is deprecated; consider using strcpy_s instead
strc