1.strcpy (copy) char* my_strcpy (CHAR*DST,&NBSP;CONST&NBSP;CHAR*SRC) { assert (DST); assert (SRC); char* cp = dst; while (*cp++ = *src++) { ; } &NBSP;RETURN&NBSP;DST;} 2.strcat (Connection) Char* my_strcat (CHAR*DST,&NBSP;CONST&NBSP;CHAR*SRC) { assert (DST); assert (SRC); char* cp = dst; while (*cp != ') { cp++; } while (*cp++ = *src++) { ; } return dst;} 3.STRCMP (Compare) int my_strcmp (CONST&NBSP;CHAR*DST,&NBSP;CONST&NBSP;CHAR*SRC) { assert (DST); assert (SRC); int ret = 0; while (! ( ret=* (unsigned char*) dst - * (unsigned char*) src) && *dst) { dst++; src++; } if (ret > 0) { ret = 1; } else if (ret < 0) { &nbsP;ret = -1; } return ret;} 4.strstr (looking for substrings) Char* my_strstr (CONST&NBSP;CHAR*DST,&NBSP;CONST&NBSP;CHAR*SRC) {&NBSP;CHAR*&NBSP;CP = (char*) src; char*s1; char*s2; if (!*DST) { return ((Char *) src); } while (*CP) { s1 = cp; s2 = (char*) DST ; while (! ( *S1-*S2) &&*s1&&s2) { s1++; s2++; } if (!*S2) { return (CP); } cp++; } return (NULL);} 5.memcpy (memory copy, non-overlapping) void* my_memcpy (Void *dst, const void*src, size_t count) { assert (DST); assert (SRC); char*ret = (char*) dst; while (count--) { * (char*) dst = * (char*) src; dst = (char*) dst + 1; src = (char*)Src + 1; } return ret;} 6.memmove (memory move for overlapping copies)//memory movement There are two cases: if there is a string: abcdefgh If dst<src, that is, DST is in front of SRC, there is no overlap. If DST>SRC, that is, DST is behind SRC, at this point if srt+count<dst, there is no overlap. If DST>SRC, that is, DST after SRC, at this time if SRT+COUNT>DST, then there is overlap, at this time from the back forward, from Src+count to start copying to Dst+count. Void *my_memmove (Void*dst, const void*src, size_t count) { assert (DST); ASSERT (SRC); void *ret = dst; if ((DST&NBSP;<&NBSP;SRC) | | ((char*) src + count) < (char*) DST) //memory does not overlap and is copied sequentially { while (count--) { * (char*) dst = * (char*) src; dst = (char*) dst + 1; src = (char*) src + 1; } } else //overlap, copy { dst = reverse (char*) dst + count - 1; src = (char*) src + count - 1; while (count-- ) { * (char*) dst = * (char*) src; dst = (char*) dst - 1; src = (char*) Src - 1; } } return ret;} 7.memset (Memory setting) Void* my_memset (void*dst, int val, size_t count) { void*start = dst; while (count--) { * (char*) dst = (char) val; dst = (char*) Dst + 1; } return start;}
This article is from the "printf Return Values" blog, so be sure to keep this source http://10741125.blog.51cto.com/10731125/1782572
Simulating the implementation of partial library functions (Strcpy,strcmp,strcat,strstr,memcpy,memmove,memset)