c語言實現字元串拷貝函數的幾種方法

首先是使用庫函數
比如下面代碼

void ourStrCopy(char S1[] , char S2[]){ strcpy(S1, S2); //該函數還有另一個版本可以按長度截取 }

還有一個函數是memcpy,這個是內存拷貝,原型是

void memcpy(void *dest, const void *src, size_t n); 需要注意的是這個函數第一個和第二個指針都是void型且第二個指針不能被修改,第三個參數是需要拷貝的內存長度按位元組記。

然後是用指針引用,注意這個並非賦值,而是引用,這種操作需要注意內存。

char s1[] = "abcdefg";//定義一組字元串char *s2 = s1;//按照指針拷貝字元串

第三種方法就是直接賦值了

void outStrCopy(char s1[] , char s2[]){ int len1 = strlen(s1);//獲取第一個字元串的長度 int len2 = strlen(s2);//獲取第二個字元串的長度 int len = 0; //字元串總長度 if(len1 <= len2){ len = len2; //選擇COPY的長度 }else{ len = len1; } for(int i = 0 ; i < len ; i++){ s1[i] = s2[i]; //實現數據拷貝 }}

❷ C語言中如何編寫一個字元串復制函數,並在主函數中調用它。

||

#include<stdio.h>

char *custom_cpy(char *to,char *from);

int main(int argc,char *argv[]){

char *from = "i like c langanger";

char to[30];

char *ptr = custom_cpy(to,from);

printf("%s,%s ",ptr,to);

return 0;

}

char *custom_cpy(char *to,char *from){

if(to == NULL || from == NULL){

return NULL;

}

char *p = to;

for(;*from!='';from++,to++){

*to = *from;

}

*to = '';//拷貝完畢之後一定要加一個結束符號

return p;

}

(2)c語言拷貝函數擴展閱讀:

字元串相關函數應用:

1. 連接運算 concat(s1,s2,s3…sn) 相當於s1+s2+s3+…+sn。

例:concat(『11』,'aa』)='11aa』;

2. 求子串。 Copy(s,I,I) 從字元串s中截取第I個字元開始後的長度為l的子串。

例:(『abdag』,2,3)=』bda』;

3. 刪除子串。過程 Delete(s,I,l) 從字元串s中刪除第I個字元開始後的長度為l的子串。

例:s:=』abcde』;delete(s,2,3);結果s:=』ae』;

4. 插入子串。 過程Insert(s1,s2,I) 把s1插入到s2的第I個位置

例:s:=abc;insert(『12』,s,2);結果s:=』a12bc』;

5. 求字元串長度 length(s) 例:length(『12abc』)=5;

網路-字元串

❸ 用C語言實現字元串拷貝函數有幾種方式

1 用庫函數
比如 用strcpy
strncpy
memcpy
sprintf
這些都可以實現

2 用自定義函數。
自己寫
方法多種多樣
演算法少說也有幾十種 實現出來 加上變種,幾百種都不多。

❹ 編寫C語言的字元串拷貝函數

整體使用示例如下:

其中char * str( char *strDest, const char *strSrc )即str實現方法,並在main函數中進行了測試!

❺ 用c語言寫一個字元串拷貝函數

*q='\0';
return
*q;
所以函數返回的是『\0』。然後主函數裡面你的printf列印輸出的是函數的返回值而不是num這個數組,你把%s後面的參數改成num就行了。

❻ c語言中如何像用函數strcpy復制字元串一樣復制數字,如復制30。

||

寫一個函數即可。比如,可以寫如下的一個函數:

intintncpy(int*des,int*src,intlen)
{
inti;
if(des==NULL||src==NULL||len<=0)
return-1;
for(i=0;i<len;i++)
{
*des++=*src++;
}
return0;
}

說明:

  1. 為了適合大多數情版況,不方便像strcpy()一樣權,用「」自動判別字元串結尾。因此用類似於strncpy()定長度的方式,進行數字拷貝。

  2. 其它數字類型,比如float, double, 都可以按照類似的方式,編制對應的ncpy()程序。

可以繼續交流,謝謝。

❼ c語言strcpy()用法

1、strcpy函數是復制字元串的,接受兩個參數,一個是被復制字元串,另一個新字元串。具體的用法,首先打開編輯器新建一個c語言的程序文件,寫入頭文件和主函數:

❽ 自定義c語言字元串拷貝函數strcpy

char* mystr(char *dest,const char *src)
{
int lens=0,i,j;
while(src[j]!='\0')
{
lens++;
j++;
}
for(i=0;i<=lens;i++)
dest[i]=src[i];
return dest;
}
我不知道參數加
const這個對不對我說下程序中的錯誤。
j沒有初始化就使用,錯誤
你怎麼知道dest的長度就比src大了,是不是也要計算下dest的長度,然後條件寫i<=len1&&i<=len2

❾ C語言串拷貝(strcpy)和內存拷貝(memcpy)函數有什麼不同

strcpy()函數只能拷貝字元串。strcpy()函數將源字元串的每個位元組拷貝到目錄字元串中,當遇到字元串末尾的null字元(\0)時,它會刪去該字元,並結束拷貝。 memcpy()函數可以拷貝任意類型的數據。因為並不是所有的數據都以null字元結束,所以你要為memcpy()函數指定要拷貝的位元組數。 在拷貝字元串時,通常都使用strcpy()函數;在拷貝其它數據(例如結構)時,通常都使用memcpy()函數。以下是一個使用strcpy()函數和memcpy()函數的例子: #include <stdio. h> #include <string. h> typedef struct cust-str {int id ;char last_name [20] ; char first_name[l5];} CUSTREC;void main (void); void main (void){char * src_string = "This is the source string" ; char dest_string[50]; CUSTREC src_cust; CUSTREC dest_cust; printf("Hello! I'm going to src_string into dest_string!\n"); / * Copy src_ string into dest-string. Notice that the destination string is the first argument. Notice also that the strcpy() function returns a pointer to the destination string. * / printf("Done! dest_string is: %s\n" , strcpy(dest_string, src_string)) ; printf("Encore! Let's one CUSTREC to another. \n") ; prinft("I'll src_cust into dest_cust. \n"); / * First, intialize the src_cust data members. * / src_cust. id = 1 ; strcpy(src_cust. last_name, "Strahan"); strcpy(src_cust. first_name, "Troy"); / * Now, Use the memcpy() function to the src-cust structure to the dest_cust structure. Notice that, just as with strcpy(), the destination comes first. * / memcpy(&dest_cust, &src_cust, sizeof(CUSTREC));