#include <stdio.h>
// 实现自定义的strcpy函数
char* mystrcpy(char *dest, const char *src) {
char *original_dest = dest;
while (*src) {
*dest = *src;
dest++;
src++;
}
*dest = '\0';
return original_dest;
}
// 实现自定义的strcmp函数
int mystrcmp(const char *s1, const char *s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(unsigned char *)s1 - *(unsigned char *)s2;
}
int mystrlen(const char *s) {
int count = 0;
while (*s) {
count++;
s++;
}
return count;
}
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
char str3[20] = "";
printf("mystrcpy: %s\n", mystrcpy(str3, str1));
printf("mystrcmp: %d\n", mystrcmp(str1, str2));
return 0;
}
2222222222
最新推荐文章于 2024-09-04 21:39:45 发布