1. strcpy
char *strcpy(char *strDst, char *strSrc) { assert(strDst != NULL && strSrc != NULL); char *tmpDst = strDst; while((*tmpDst++ = *strSrc++) != '\0') ; return strDst; }
2. strcat
char *strcat(char *strDst, char *strSrc) { assert(strDst != NULL && strSrc != NUULL); char *tmpDst = strDst; while(*tmpDst) //不可使用while(*tmpDst++) { tmpDst++; } while((*tmpDst++ = *strSrc++) != '\0') ; return strDst; }
3. strcmp
int strcmp(const char *str1, const char *str2) { assert(str1 != NULL && str2 != NULL); int ret; while(!(ret = *(unsigned char*)str1 - *(unsigned char*)str2) && *str1 && *str2) { str1++; str2++; } if (ret < 0) ret = -1; else if (ret == 0) ret = 0; else ret = 1; return ret; }
4. strlen
int strlen(const char *str) { const char *tmpStr; for (tmpStr = str; *tmpStr++;) ; return (tmpStr - str -1); }