亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

不用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?D*/
int i;
for(i=0;i<T.len;i++)
S.element[i]=T.element[i];
S->len=T.len;
}



查看完整回答
反對 回復 2019-03-20
  • 4 回答
  • 0 關注
  • 1622 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號