strcpy strlen 实现
1. strcpy:字符串拷贝函数,无边界,一直拷贝到字符串结束符 '\0' 为止。
char* strcpy(char* strDest, const char* strSrc);
代码实现:
1 #include <assert.h> 2 3 char* strcpy(char* strDest, const char* strSrc) 4 { 5 assert((strDest != NULL) && (strSrc != NULL)); 6 char* address = strDest; 7 8 while((*strDest++ = *strSrc++) != '\0'); 9 10 return address; 11 }
2. strlen:求字符串长度,不包括字符串结束符 '\0'。
int strlen(const char* str);
代码实现:
1 #include <assert.h> 2 3 int strlen(const char* str) 4 { 5 assert(str != NULL); 6 int len = 0; 7 8 while(*str++ != '\0') 9 { 10 len++; 11 } 12 13 return len; 14 }