面试之自定义字符串操作
面试之自定义字符串操作
-
strcpy
#include <assert.h> char *strcpy(char *dest, const char *src) { assert( (dest != NULL) && (src != NULL); char *address = dest; while((*dest++ = *src++) != ''); return address; }
-
strcmp
#include <assert.h> int strcmp(const char *str1, const char *str2) { assert((str1 != NULL) && (str2 != NULL)); for (; (*str1) && (*str2) && (*str1 == *str2);++ str1,++ str2); return (*str1 - *str2); }
-
strcat
char *m_strcat(char *dest, const char *src) { assert((dest != NULL) && (src != NULL)); char *add = dest; while (*dest != '\0') ++dest; while (*dest++ = *src++); return add; }
-
strlen
unsigned int m_strlen(const char *str) { assert(str != NULL); int len = 0; while (*str != '\0') { ++str; ++len; } return len; }