Use C language. Analog Implementation Strcpy,strcat,strcat,memcpy,memmove

Source: Internet
Author: User

1. Analog implementation strcpy#include<stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> char *my_strcpy (CHAR&NBSP;*DST,&NBSP;CONST&NBSP;CHAR&NBSP;*SRC) {assert (dst != null); ASSERT (src  != null);char *ret = dst;while  ((*dst++ = *src++)  !=  ' + '); Return ret;} Int main () {char arr1[] =  "Hello world!!!"; char arr2[20];my_strcpy (ARR2,&NBSP;ARR1);p rintf ("%s\n", &NBSP;ARR2); System ("Pause");       return 0;} 2. Analog implementation strcat#include<stdio.h> #include <assert.h> #include <stdlib.h>char *strcat (char &NBSP;&NBSP;*DEST,&NBSP;CHAR&NBSP;CONST&NBSP;*SRC) { assert (dest); assert (SRC);char *temp =  dest;while  (*dest) {dest++;} while  (*SRC) {*dest++ = *src++;} *dest =  ' + '; return temp;} Int main () {char arr[50] =  "i come from china!!"; char *p =  " me too!! "; strcat (arr, p);p rintf ("%s",  arr); System ("pause");    return 0;} 3. Analog implementation of strcat#include<stdio.h> #include <assert.h> #include <stdlib.h>int my_strcmp (const &NBSP;CHAR&NBSP;*ARR1,&NBSP;CONST&NBSP;CHAR&NBSP;*ARR2) {assert (ARR1); assert (ARR2);while  (*arr1 == &NBSP;*ARR2) {if  (*arr1 ==  ') {return 1;}     arr1++;arr2++;} RETURN&NBSP;*ARR1&NBSP;-&NBSP;*ARR2;} Int main () {char *s1 =  "i am a student!"; char *s2 =  "i am a student!"; INT&NBSP;RET=MY_STRCMP (S1,&NBSP;S2);if  (ret == 1) {printf ("Two strings are the same! \ n ");} Else{printf ("Two string difference:%d\n",  ret);} System ("pause"); return 0;} 4. Analog implementation memcpy#include<stdio.h> #include <string.h> #include <assert.h> #include <stdlib.h> void *my_memcpy (void *dest, const void *src, size_t n) {assert (dest); Assert ( src);char *p =  (char *) dest;const char *q =  (char *) src;while  (n--) {*p = *q;p++;q++;} Return dest;} Int main () {int arr1[100] = { 1,2,3,4,5,6,7,8,9,10,11 };int arr2[100] =  { 0 };int i = 0;int num;printf ("Please enter the number of copies to be copied:"); scanf ("%d",  &num); my _memcpy (Arr2, arr1, 4*num);for  (i = 0; i < num; i++) {printf ("% d  ",  arr2[i]);} printf ("\ n"); system ("pause"); return 0;} 5. Analog implementation memmove#include<stdio.h> #include <string.h> #include <assert.h> #include <stdlib.h> void *my_memcpy (void *dest, const void *src, size_t n) {assert (dest); Assert ( SRC);char *p =  (char *) dest;const char *q =  (char *) src;if  ( P > q && p < q + n) {while  (n--) {  * (P  + n)  = * (q + n);}} else{while  (n--) {*p = *q;p++;q++;}} Return dest;} Int main ( ) {int arr[] = { 1, 2, 3, 4, 5, 6, 7,  8, 9, 10 };int i = 0;my_memcpy (arr+4, arr+2, 20);for  (i  = 0; i < 10; i++) {printf ("%d ",  arr[i]);} printf ("\ n"); system ("pause"); return 0;}


Use C language. Analog Implementation Strcpy,strcat,strcat,memcpy,memmove

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.