華文網

C|自訂庫函數strcpy(字串複製)

在C或C++string庫中有字元複製函數strcpy,可以完成字元複製功能,當然也可以自訂。

代碼:

運行結果:

str2 string:Hello World

str1 string:Hello World

源碼:

#include

#include

/*==================================================================

函數功能:將字元陣列str2複製給一個空字元陣列

函數輸入:空字數陣列str1、字元陣列str2

函數輸入:str1

====================================================================*/

char * mystrcpy(char *str1,const char *str2)

{

char *p = str1;

if(p == NULL || str2 == NULL)

{

printf("The string is error! "); /*非法的字串拷貝*/

exit(0);

}

while(*str2 != '