模拟实现:
strstr
strcpy
strlen
strcat
strcmp
memcpy
memmove
================================
1 strstr 字符串中查找子字符串
char * my_strstr(const char *dest, const char *src){ const char *ret = dest; const char *p = dest; const char *q = src; assert(dest != NULL && src != NULL);while(ret){q = src;while(*p == *q){p++;q++;}if(*q == '\0'){return (char *)ret;}else{p = ++ret;}}return NULL;}
===========================================
2 strcpy 字符串复制
char *my_strcpy(char *dest,const char *src){char *ret = dest;assert(dest);assert(src);while(*dest++ = *src++){;}return ret;}
==========================================
3 strlen 测字符串长度
int my_strlen(const char *s){const char *p = s;while(*s){s++;}return s - p;}
==========================================
4 strcat 连接字符串
char * my_strcat(char *dest, const char *src){char *ret = dest;assert(dest);assert(src);while(*dest){dest++;}while(*dest++ = *src++){;}return ret;}
=========================================
5 strcmp 字符串复制
int my_strcmp(const char *s1, const char *s2){assert(s1);assert(s2);while(*s1 == *s2){if(*s1 == '\0'){return 0;}s1++;s2++;}return (*s1 - *s2 > 0 ? 1 : -1);}
============================================
6 memcpy 内存复制
void * my_memcpy(void *buf1,const void *buf2,int count){void *ret = buf1;char *dest = (char *)buf1;char *src = (char *)buf2;assert(buf1);assert(buf2);while(count--){*dest++ = *src++;}return ret;}
=====================================================
7 memmove 内存移动
void *my_memmove(void *buf1, const void *buf2,int count){void *ret = buf1;char *dest = (char *)buf1;char *src = (char *)buf2;assert(buf1);assert(buf2);if(dest > src && dest < src + count)//重叠时从后向前移动{while(count--){*(dest + count) = *(src + count);}}else {while(count--){*dest++ = *src++;}}return ret;}