首页 > 代码库 > 我要好offer之 str/mem系列手写代码
我要好offer之 str/mem系列手写代码
1. str*系列手写代码
a. 一定要注意末尾‘\0‘的处理,切记切记
b. 一定要对输入做有效性判断,多用断言就是了
int Strlen(const char* str) { assert(str != NULL); const char* tmp = str; while (*tmp != ‘\0‘) { ++tmp; } return tmp - str;}char* Strcpy(char* dst, const char* src) { assert(dst != NULL && src != NULL); char* tmp = dst; while (*src != ‘\0‘) { *tmp++ = *src++; } *tmp = ‘\0‘; return dst;}char* Strncpy(char* dst, const char* src, int len) { assert(dst != NULL && src != NULL && len >= 0); char* tmp = dst; for (; len > 0 && *src != ‘\0‘; --len) { *tmp++ = *src++; } for (; len > 0; --len) { *tmp ++ = ‘\0‘; } return dst;}char* Strcat(char* dst, const char* src) { assert(dst != NULL && src != NULL); char* tmp = dst; while (*tmp != ‘\0‘) { ++tmp; } while (*src != ‘\0‘) { *tmp++ = *src++; } *tmp = ‘\0‘; return dst;}char* Strncat(char* dst, const char* src, int len) { assert(dst != NULL && src != NULL && n >= 0); char* tmp = dst; while (*tmp != ‘\0‘) { ++tmp; } for (; len > 0 && *src != ‘\0‘; --len) { *tmp++ = *src++; } *tmp = ‘\0‘; return dst;}int Strcmp(const char* str1, const char* str2) { assert(str1 != NULL && str2 != NULL); for (; *str1 == *str2; ++str1, ++str2) { if (*str1 == ‘\0‘) { return 0; } } if (*(unsigned char*)str1 < *(unsigned char*)str2) { return -1; } else { return 1; }}int Strncmp(const char* str1, const char* str2, int len) { assert(str1 != NULL && str2 != NULL && len >= 0); for (; len > 0; ++str1, ++str2) { if (*str1 != *str2) { return ((*(unsigned char*)str1) < (*(unsigned char*)str2) ? -1 : 1); } else if (*str1 == ‘\0‘) { return 0; } } return 0;}char* Strchr(const char* str, int c) { assert(str != NULL); const char* tmp = str; const char ch = (const char)c; for (; *tmp != ch; ++tmp) { if (*tmp == ‘\0‘) { return NULL; } } return (char*)tmp;}char* Strstr(const char* str1, const char* str2) { assert(str1 != NULL && str2 != NULL); if (*str2 == ‘\0‘) return str1; const char* tmp1 = str1; const char* tmp2 = str2; int len1 = Strlen(str1); int len2 = Strlen(str2); int i = 0; for (tmp1 = str1; i <= len1 - len2 && *tmp1 != ‘\0‘; ++tmp1, ++i) { if (*tmp1 != *tmp2) continue; const char* cur1 = tmp1; const char* cur2 = tmp2; while (*cur1 == *cur2) { ++cur1; ++cur2; if (*cur2 == ‘\0‘) { return (char*)tmp1; } } } return NULL;}
2. mem系列手写代码
一定要对输入做有效性判断,多用断言就是了
void* Memchr(const void* src, int c, int len) { assert(src != NULL && len >= 0); const unsigned char ch = c; const unsigned char* tmp = (const unsigned char*)src; for (; len > 0; --len, ++tmp) { if (*tmp == ch) { return (void*)tmp; } } return NULL;}int Memcmp(const void* s1, const void* s2, int len) { assert(s1 != NULL && s2 != NULL); const unsigned char* tmp1 = (const unsigned char*)s1; const unsigned char* tmp2 = (const unsigned char*)s2; for (; len > 0; --len, tmp1++, tmp2++) { if (*tmp1 != *tmp2) { return ((*tmp1 < *tmp2) ? -1 : 1); } } return 0;}void* Memcpy(void* dst, const void* src, int len) { assert(dst != NULL && src != NULL && len >= 0); char* dstTmp = (char*)dst; const char* srcTmp = (const char*)src; for (; len > 0; --len, ++dstTmp, ++srcTmp) { *dstTmp = *srcTmp; } return dst;}void* Memmove(void* dst, const void* src, int len) { assert(dst != NULL && src != NULL && len >= 0); char* dstTmp = (char*)dst; const char* srcTmp = (const char*)src; if (dstTmp > srcTmp && dstTmp < srcTmp + len) { for (srcTmp += n, dstTmp += n; len > 0 ; --len, --srcTmp, --dstTmp) { *dstTmp = *srcTmp; } } else { for (; len > 0; --len, ++srcTmp, ++dstTmp) { *dstTmp = *srcTmp; } } return dst;}
3. atoi函数
http://www.cnblogs.com/wwwjieo0/p/3687534.html
class Solution {public: int atoi(const char *str) { assert(str != NULL); const char* curr = str; const int maxRange = 10; int tmp = 0; int num = 0; while(isspace(*curr)) { ++curr; } const char* start = nullptr; char sign; if (*curr == ‘-‘ || *curr == ‘+‘) { sign = *curr; ++curr; start = curr; } else { start = curr; } while (isdigit(*curr)) { tmp = num; num = num * 10 + (*curr - ‘0‘); ++curr; } int len = 0; if (!isdigit(*curr)) { len = curr - start; } --curr; if (len > maxRange || num < num - *curr) { if (sign == ‘-‘) { return INT_MIN; } else { return INT_MAX; } } if (sign == ‘-‘) num = -num; return num; }};
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。