Use C language to simulate strcpy, strcat, strcat, memcpy, memmove

Source: Internet
Author: User

Use C language to simulate strcpy, strcat, strcat, memcpy, memmove

1. simulate Implementation of strcpy # include <stdio. h> # include <stdlib. h> # include <string. h> # include <assert. h> char * my_strcpy (char * dst, const char * src) {assert (dst! = NULL); assert (src! = NULL); char * ret = dst; while (* dst ++ = * src ++ )! = '\ 0'); return ret;} int main () {char arr1 [] = "hello world !!! "; Char arr2 [20]; my_strcpy (arr2, arr1); printf (" % s \ n ", arr2); system (" pause "); return 0;} 2. simulate Implementation of strcat # include <stdio. h> # include <assert. h> # include <stdlib. h> char * strcat (char * dest, char const * src) {assert (dest); assert (src); char * temp = dest; while (* dest) {dest ++;} while (* src) {* dest ++ = * src ++;} * dest = '\ 0'; return temp;} int main () {char arr [50] = "I come from china !! "; Char * p =" me too !! "; Strcat (arr, p); printf (" % s ", arr); system (" pause "); return 0 ;}3. simulate Implementation of strcat # include <stdio. h> # include <assert. h> # include <stdlib. h> int my_strcmp (const char * arr1, const char * arr2) {assert (arr1); assert (arr2); while (* arr1 = * arr2) {if (* arr1 = '\ 0') {return 1;} arr1 ++; arr2 ++;} return * arr1-* arr2;} int main () {char * s1 = "I am a student! "; Char * s2 =" I am a student! "; Int ret = my_strcmp (s1, s2); if (ret = 1) {printf (" the two strings are the same! \ N ");} else {printf (" the difference between the two strings is % d \ n ", ret);} system (" pause "); return 0;} 4. simulate the implementation of 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] = {100, 11}; int arr2 [] = {0}; int I = 0; int num; printf ("Enter the number of copies:"); 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. simulate the implementation of 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 ;}

 

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.