Strcpy and memcpy programming implementation, strcpymemcpy Programming

Source: Internet
Author: User

Strcpy and memcpy programming implementation, strcpymemcpy Programming

1 char * strcpy (char * dest, char * src) 2 {3 char * d = dest; // back up the input parameters 4 char * s = src; 5 6 int count = 0; 7 8 assert (dest! = NULL & src! = NULL); // pointer validity check
If (src = dest) return src; 11 count = strlen (src) + 1; // calculate the src String Length 12 if (count <= 1) return 0; // src string is empty 13 if (dest <src | dest> = (src + count) 14 {15 while (count --) 16 {* d ++ = * s ++;} // copy 17} 18 else {19 d = dest + count; 20 s = src + count; 21 while (count --) 22 {23 * d -- = * s --; // copy back and forth 24} 25} 26 return dest; 27}
 1 void memcpy(void *pDst,const void *pSrc,size_t size) 2 { 3   assert(pDst!=NULL && pSrc!=NULL); 4   if((pSrc<pDst)&&((char *)pSrc+size>pDst)) 5   { 6     char *pstrSrc=(char *)pSrc + size -1; 7     char *pstrDst=(char *)pDst +size-1; 8     while(size--) 9       *pstrDst-- = *pstrSrc;10   }11   else{12     char *pstrSrc=(char *)pSrc;13     char *pstrDst=(char *)pDst;14     while(size--){15       *pstrDst++ = *pstrSrc++;16    }17 }

Strcpy and memcpy have the following three differences.
1. The copied content is different. Strcpy can only copy strings, while memcpy can copy any content, such as character arrays, integers, struct, and classes.
2. The replication method is different. Strcpy does not need to specify the length. It ends with the string Terminator "\ 0" of the copied character, so it is prone to overflow. Memcpy decides the copy Length Based on its 3rd parameters.
3. Different purposes. Strcpy is usually used to copy strings, while memcpy is generally used to copy data of other types.


C ++ strcpy function implementation

/*
1123rty
1123rty
1123rty
Press any key to continue
*/
# Include <stdio. h>
Char * strcopy (char * strDest, const char * strSrc ){
Char * tempptr = strDest;
While (* tempptr ++ = * strSrc ++ );
Return strDest;
}

Int main (){
Char aa [100];
Char bb [100];
Gets (bb );
Strcopy (aa, bb );
Puts (aa );
Puts (bb );
Return 0;
}

Programming: Can I use a custom function to implement the functions of string processing functions strcat, strcpy, strcmp, strlen, and strlwr?

I don't know what strlwr is?
1. strcat
Void mycat (char * s1, char * s2)
{
While (* s1 ++ );
S1 --;
While (* s1 ++ = * s2 ++ );
}
2. strcpy
Void mycpy (char * s1, char * s2)
{
While (* s1 ++ = * s2 ++ );
}
3. strcmp
Int mycmp (char * s1, char * s2)
{
For (; * s = * t; s ++, t ++)
If (* s = 0) return 0;
Return * s-* t;
}
4. strlen
Int mylen (char * s1)
{
Char * p = s1;
While (* p ++ );
Return p-s-1;
}

Related Article

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.