02 08 字串拷貝函式的實現

2021-08-21 02:56:42 字數 1884 閱讀 9482

#define _crt_secure_no_warnings//強行去掉安全檢查

#include

#include

#include

//完美函式

//2:被呼叫函式要有返回值 成功0,失敗非0

//3:盡量不要直接使用形參(可定義臨時變數接過來)

void main1()

;int i = 0;

//int n = strlen(src);

//字串拷貝到dst裡面

//for (i; i < n; i++)

for (i = 0; src[i] != '\0'; i++)

//補結束符 不補這個會亂碼

dst[i] = '\0';

printf("%s\n", dst);

printf("\n");

system("pause");

}void my_strcpy(char *dst,char *src)

//補結束符 不補這個會亂碼

// *dst = '\0';

*(src + i) = '\0';

}void my_strcpy2(char *dst, char *src)

*dst = '\0';

}void my_strcpy3(char *dst, char *src)

*dst = '\0';

}void my_strcpy4(char *dst, char *src)

*dst = '\0';

//終結**  等於一行

//while (*dst++ = *src++);

}//成功為0  失敗非0

//1:判斷形參指標是否為null

//2:不要直接使用(指標)形參

//如果要用,可定義臨時變數

int  my_strcpy5(char *dst, char *src)

//定義乙個臨時變數

//輔助變數把形參接過來

//盡量不要直接使用形參

char *to = dst;

char *from = src;

//*dst=*src

//dst++ ,src++

//判斷*src 是否為0 ,為0跳出迴圈

while (*to++ = *from++)

printf("my_strcpy5:dst=%s\n", dst);

return 0;

}void main()

;int ret = 0;

//ret = my_strcpy5(null, src);

ret =my_strcpy5(dst,src);

//完美函式

//2:被呼叫函式要有返回值 成功0,失敗非0

//3:盡量不要直接使用形參(可定義臨時變數接過來)

if (ret != 0)

printf("%s\n", dst);

printf("\n");

//++ 在前在後的功能

int i = 0;

int n = i++;

printf("i=%d,n=%d", i, n);

//i++先引用,後自增

//i結果原來是0,先n=0,後i+1

//結果i=1  n=0

printf("\n");

int i2 = 0;

int n2 = ++i2;

printf("i2=%d,n2=%d", i2, n2);

//i2 ++先自增,後引用

//i2結果原來是0,先0+1=1; 後引用n2=i2

//先加了在引用

//結果i=1  n=1

system("pause");

}

8 字串函式

right left 可從列中選出指定數量的字元 right location,2 location 欄位 substring index 可擷取部分字段值 substring index location,1 尋找第乙個逗號,然後擷取之前的內容 substring your string,star...

6 字串函式

strcpy函式char strcpy char dest,const char src 功能 把src所指向的字串覆蓋複製到dest。返回值 dest指向的字串。注意 字串src的長度不能超過dest,否則會溢位。strncpy函式char strncpy char dest,const char...

6 8 字串函式

1 字串就是由多個字元在記憶體中連續分布組成的字元結構。字串的特點是指定了開頭 字串的指標 和結尾 結尾固定為字元 0 而沒有指定長度 長度由開頭位址和結尾位址相減得到 1 函式庫為什麼要包含字串處理函式?因為字串處理的需求是客觀的,所以從很早開始人們就在寫很多關於字串處理的函式,然後逐漸形成了現在...