1.將字串複製: 於while程式段中的ptr1修改為如ptr的遞增運算方式
2.如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | /* Ch3-4-1e.c */ /* 主程式 */ int { /* 字元陣列宣告 */ char str[LEN] = "This is a book."; char str1[LEN]; /* 字元指標 */ char *ptr = str; char *ptr1; /* 顯示字串內容 */ ptr1 = "This is a pen."; printf("str = %s\n", str); printf("ptr = %s\n", ptr); printf("ptr1 = %s\n", ptr1); /* 字串複製的迴圈 */ printf("將字串str複製到str1: \n"); ptr1 = str1; while { *ptr1++ = *ptr++; } *ptr1++ = '\0'; printf("str1 = %s\n", str1); printf("ptr1 = %s\n", ptr1); system("PAUSE"); return 0; } |
執行結果:
str = This is a book.
ptr = This is a book.
ptr1 = This is a pen.
將字串str複製到str1:
str1 = This is a book.
ptr1 = This is a book.
請按任意鍵繼續 . . .
評分: ★★★★☆
回覆刪除Excellent !