Simulating the implementation of partial library functions (Strcpy,strcmp,strcat,strstr,memcpy,memmove,memset)

Source: Internet
Author: User

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&GT;SRC, that is, DST is behind SRC, at this point if srt+count<dst, there is no overlap. If DST&GT;SRC, that is, DST after SRC, at this time if SRT+COUNT&GT;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;&LT;&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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.