Strcpy实现:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<assert.h> char*my_strcpy(char*dest, const char*src) { char*ret = NULL; assert(dest); assert(src); ret = dest; while (*dest++ = *src++) {} return ret; } int main() { char arr[10]; my_strcpy(arr, "bit_tech"); printf("%s\n", arr); system("pause"); return 0; }
结果:
bit_tech
Strcmp实现:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<assert.h> int*my_strcmp(const char*str1, const char*str2) { assert(str1); assert(str2); while (*str1== *str2) { if (*str1 == ‘\0‘) { return 0; } str1++; str2++; } return*str1 - *str2; } int main() { char*p1 = "abcdef"; char*p2 = "acbcde"; int ret=my_strcmp(p1,p2); if (ret > 0) { printf("p1>p2\n"); } else if (ret = 0) { printf("p1=p2\n"); } else printf("p1<p2\n"); system("pause"); return 0; }
结果:
p1<p2
Memmove实现:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<assert.h> void*my_memmove(void*dest, const void*src,size_t count) { char*ret = NULL; char*pdest = (char*)dest; char*psrc = (char*)src; ret = dest; assert(dest); assert(src); if ((pdest<psrc + count) && (pdest>psrc))//从后向前拷贝 { while (count--) { pdest[count] = psrc[count]; } } else { while (count--) { *(pdest++) = *(psrc++); } } return ret; } int main() { int i = 0; int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; my_memmove(arr + 3, arr + 1, 4 * 4); for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) { printf("%d ", arr[i]); } printf("\n"); system("pause"); return 0; }
结果:1 2 3 2 3 4 5 8 9
时间: 2024-10-29 19:10:55