为了账号安全,请及时绑定邮箱和手机立即绑定

不用strcpy实现字符串的复制?

不用strcpy实现字符串的复制?

Yii
明月笑刀无情 2019-02-06 16:07:46
不用strcpy实现字符串的复制
查看完整描述

4 回答

?
慕的地6264312

TA贡献1817条经验 获得超6个赞

#include "stdio.h"
#include "string.h"
#include "malloc.h"

void StringCopy(const char *strSource, char *strDestination);
void main()
{
char *strSource={"hello world !"};
char *strDestination;
//给strDestination·分配内存空间,否则运行时会有异常发生
strDestination = (char *) malloc (strlen(strSource) + 1);
if (strDestination == NULL)
{
printf("内存分配失败 ");
return ;
}
StringCopy(strSource, strDestination);
printf("%s ",strDestination);
free(strDestination);
}
void StringCopy(const char *strSource, char *strDestination)
{
int i=0; //地址计数器
while(*(strSource+i)!='\0')
{
//不能写成 strDestination++的形式,因为strDestination是字符串的首地址
*(strDestination+i)=*(strSource+i);
i++;
}
*(strDestination+i)='\0';//给拷贝后的字符串加上结束符
}



查看完整回答
反对 回复 2019-03-20
?
HUWWW

TA贡献1874条经验 获得超12个赞

假设有2个字符串char* dst, char* src,将src中的字符复制到dst中
while( *dst++ = *src++ )
;
就可以了,其实就是strcpy函数的具体实现过程

查看完整回答
反对 回复 2019-03-20
?
白衣染霜花

TA贡献1796条经验 获得超10个赞

#define maxsize 50
#define true 1
#define false 0
#define other -1
typedef struct
{
char element[maxsize];
int len;
}sstring;
void strcopy(sstring *S,sstring T)
{/*½«´®TµÄÖµ¸´ÖƵ½´®SÖÐ*/
int i;
for(i=0;i<T.len;i++)
S.element[i]=T.element[i];
S->len=T.len;
}



查看完整回答
反对 回复 2019-03-20
  • 4 回答
  • 0 关注
  • 1469 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信