复制源字符串到目标字符串!

crossingdragons 2003-09-14 04:22:26
复制源字符串到目标字符串!包括终止null,假设目标字符串有足够的空间!
int StrCpy(char *strDest, char *strSrc)
...全文
778 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
dddd8888 2003-09-15
  • 打赏
  • 举报
回复
char * strcpy(char * dest,const char *src)
{
ASSERT(dest!=NULL && src!=NULL);
char *tmp = dest;

while ((*dest++ = *src++) != '\0')
/* nothing */;
return tmp;
}
wgzh 2003-09-15
  • 打赏
  • 举报
回复
to TianGuangZao(天光早)
确实如你所说
“while ((*dest++ = *src++) != '\0')”
拷贝是考虑到了 '\0';
但是这种写法比较容易看错,以为没有考虑到'\0'(我初一看就这么认为)。
个人还是喜欢下面这种结构,直观(在不影响性能的情况下这点也很重要)!
while( *strSource != '\0' )
{
*strDestination = *strSource;
strDestination++;
strSource++;
}
*strDestination = '\0';
TianGuangZao 2003-09-14
  • 打赏
  • 举报
回复
to wgzh(火鸟):
完全同意。
不过上面这个例子是用
char *src = "hello world!";
申明的,结尾带了 '\0'.
while ((*dest++ = *src++) != '\0')
拷贝是考虑到了 '\0'

如果是 char src[] = "hello world!" 结尾也带了 '\0'
只有 char src[12] = "hello world!" 就要考虑了。
pzytony 2003-09-14
  • 打赏
  • 举报
回复
可以使用memcpy ,这样拷贝的字节的数量就确定了——

函数名: memcpy
功 能: 从源source中拷贝n个字节到目标destin中
用 法: void *memcpy(void *destin, void *source, unsigned n);
程序例:

#include <stdio.h>
#include <string.h>
int main(void)
{
char src[] = "******************************";
char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709";
char *ptr;
printf("destination before memcpy: %s\n", dest);
ptr = memcpy(dest, src, strlen(src));
if (ptr)
printf("destination after memcpy: %s\n", dest);
else
printf("memcpy failed\n");
return 0;
}


wgzh 2003-09-14
  • 打赏
  • 举报
回复
char *strcpy(char *strDestination,const char *strSource)
{
char *p = strDestination;
assert( strDestination != NULL && strSource != NULL );
while( *strSource != '\0' )
{
*strDestination = *strSource;
strDestination++;
strSource++;
}
*strDestination = '\0';
//这句一定要记得加上,楼上各位都忘了这条语句。不然打印出来的字符串
//可能有些不是想要的东西(因为没有拷贝字符串结束符)。
return p;
}
TianGuangZao 2003-09-14
  • 打赏
  • 举报
回复
to itaolu(老罗):
char *dest;
char *src = "hello world!";

dest = (char *)malloc(strlen(src)+1);

while (*dest++ = *src++) ; // 关键!

free(dest);

代码有点小问题,最后一句 free(dest) 太草率。
#include <stdio.h>

int main()
{
char *dest;
char *src = "hello world!";

dest = (char *)malloc(strlen(src)+1);
if ( dest == NULL )
{
printf ( "No enough menory to allocate!");
exit (1);
}

char *tmp = dest;
while ((*tmp++ = *src++) != '\0')
;
printf ("%s\n", dest);

free(dest);
}

TianGuangZao 2003-09-14
  • 打赏
  • 举报
回复

/**
* strcpy - Copy a %NUL terminated string
* @dest: Where to copy the string to
* @src: Where to copy the string from
*/
char * strcpy(char * dest,const char *src)
{
char *tmp = dest;

while ((*dest++ = *src++) != '\0')
/* nothing */;
return tmp;
}

这是 gcc 3.3 中 strcpy 的实现,一般还在头文件中带异常抛出。
个人认为还是尽量用现成的比较好。

strcpy/memcpy 都工作的不错,侧重面稍不同,更喜欢后者。

sprintf 带格式化输出,也是可以,就是速度相对慢点。
sprintf(destination, "%s", source);

sprintf(destination, source);
itaolu 2003-09-14
  • 打赏
  • 举报
回复
其实关键部分只要一句就够了:

char *dest;
char *src = "hello world!";

dest = (char *)malloc(strlen(src)+1);

while (*dest++ = *src++) ; // 关键!

free(dest);

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧