首页 > 代码库 > strcat strcpy strlen

strcat strcpy strlen

 1 char * strcat(char * dest, const char * src) {
 2     assert(dest != NULL && src != NULL);
 3     char * p = dest;
 4     while (*p)
 5         p++;
 6     while (*p++ = *src++);
 7     return dest;
 8 }
 9 
10 int strlen(const char * src) {
11     int count = 0;
12     while (*src++)
13         count++;
14     return count;
15 }
16 
17 char * strcpy(char * dest, const char * src) {
18     assert(dest != NULL && src != NULL);
19     char * p = dest;
20     while (*p++ = *src++);
21     return dest;
22 }

其中要注意:

1 while(*p++); // P最后指向‘\0’的下一位置
2 while (*p) p++; // p最后指向‘\0‘