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

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

具体程序??
是用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
#include

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
strcat( string,s2 ); // C4996
// Note: strcat is deprecated; consider using strcat_s instead
strcat( string, "and " ); // C4996
strcat( string, "strcat!" ); // C4996
printf( "String = %s\n", string );
}
← 上一篇: 下一篇: →