Simulate strcpy, strstr, and strcat functions in C Language

Source: Internet
Author: User

Simulate strcpy, strstr, and strcat functions in C Language
In C, to simulate the implementation of these database functions, it is to test the pointer passing involved in the function call. The Code is as follows, which is only for reference to the strcpy function:

# Include <assert. h> char * my_strcpy (char * dest, const char * src) {assert (dest); // assert (src); char * pa = dest; while (* dest ++ = * src ++); return pa ;}

 

Strstr function:
Int my_strstr (const char * str1, const char * str2) {assert (str1); assert (str2); int I = 0; char * p1 = str1; char * p2 = str2; while (* p1! = '\ 0' & * p2! = '\ 0') {if (* p1 = * p2) {p2 ++; p1 ++;} else {p2 = str2; p1 = str1 + I; I ++ ;}}if (* p2 = '\ 0') {return 1 ;}elsereturn 0 ;}

 

Strcat function:
Char * my_strcat (char * str1, const char * str2) {assert (str1); assert (str2); char * pa = str1; while (* pa) {pa ++;} while (* pa ++ = * str2 ++); return str1 ;}

 

The strcat function does not support overlapped copying. For example, if the string "1234567" is to be copied from the second digit to the fourth digit, the compiler reports a memory error, exercise caution when using the SDK.

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.