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.