1、模拟strlen(临时变量法)
#include <stdio.h>
#include <assert.h>
int my_strlen(const char* str);
int main() {char str[] = "abcdefh";int ret = my_strlen(str);printf("%d", ret);return 0;
}
int my_strlen( const char* str)
{assert(str != NULL);int count = 0;while (*str != '\0'){count++;str++;}return count;
}
2、模拟strlen(指针减指针)
#include <stdio.h>
#include <assert.h>
int my_strlen(const char* str);
int main()
{char str[] = "abcdfef";int ret = my_strlen(str);printf("%d", ret);return 0;
}
int my_strlen(const char* str)
{assert(str != NULL);char* p = str;while (*p != '\0'){p++;}return p - str;}
3、模拟strlen(函数递归)
#include <stdio.h>
#include <assert.h>
int main()
{char str[] = "abcdef";int ret = my_strlen(str);printf("%d", ret);return 0;
}
int my_strlen(const char* str)
{if (*str == '\0')return 0;elsereturn 1 + my_strlen(str + 1);
}
4、模拟实现strcpy
#include <stdio.h>
#include <assert.h>
char* my_strcpy(char* dest, const char* src);
int main()
{char str1[30] = "abcdef";char* p = "hig";char* a = my_strcpy(str1, p);printf("%s\n", str1);printf("%s", a);return 0;
}
char* my_strcpy(char* dest, const char* src)
{assert(dest != NULL);assert(src != NULL);char* p = dest;while (*src){*dest = *src; dest++;src++;}*dest = *src;return p; //虽然内存中的内容变了,但是地址还是那个地址,没有变。
}
5、模拟实现strcat
#include <stdio.h>
#include <assert.h>
char* my_strcat(char* dest, char* src);
int main()
{char str1[20] = "majun ";char str2[] = "jindachang";char* p = my_strcat(str1, str2);printf("%s", p);return 0;
}
char* my_strcat(char* dest, const char* src)
{assert(dest);assert(src);char* p = dest;while (*dest != '\0'){dest++;}//dest++; //必须把\0改掉,不然打印的时候只打印前半部分while (*dest++ = *src++){;}return p;
}
6、模拟实现strcmp
#include <stdio.h>
#include <assert.h>
int compare(char* str1, char* str2);
int main()
{char str1[] = "abcdeg";char str2[] = "abcdeg";int ret = compare(str1, str2);if (ret > 0){printf("str1>str2\n");}else if (ret == 0){printf("str1=str2\n");}else{printf("str1<str2");}return 0;
}
int compare(char* str1, char* str2)
{while (*str1 == *str2){if (*str1 == '\0' ) //这条判断要在++之前{return 0;}str1++;str2++;}return (*str1 - *str2);
}
7、模拟实现strncpy
#include <stdio.h>
#include <assert.h>
char* my_strncpy(char* dest, const char* src, int num);
int main()
{char str[20] = "sjah";char* p = "m";my_strncpy(str, p, 2);printf("%s", str);return 0;
}
char* my_strncpy(char* dest, const char* src, int num)
{assert(dest);assert(src);char* p = dest;for (int i = 0; i < num; i++){if (src != '\0') //scr没有遇到\0{*dest++ = *src++;}else{break;}}return p;
}
8、模拟实现strncat
#include <stdio.h>
#include <assert.h>
char my_strncat(char* dest, const char* src, int num);
int main()
{char str[20] = "majun ";char* p = "jin da chang";my_strncat(str, p, 3);printf("%s", str);return 0;
}
char my_strncat(char* dest,const char* src, int num)
{while (*dest != '\0'){dest++;}for (int i = 0; i < num; i++){if (src != '\0') //scr没有遇到\0{*dest++ = *src++;}else{break;}}
}
9、模拟实现strncmp
#include <stdio.h>
#include <assert.h>
int my_strncmp(char* str1, char* str2, int num);
int main()
{char str1[] = "abcdef";char str2[] = "abfse";int ret = my_strncmp(str1, str2, 3);if (ret > 0){printf("str1>str2\n");}else if (ret == 0){printf("str1=str2\n");}else{printf("str1<str2");}return 0;
}
int my_strncmp(char* str1, char* str2, int num)
{int count = 1;while (*str1 == *str2){if (count == num){return 0;}if (*str2 == '\0'){return 0;}str1++;str2++;count++;}return *str1 - *str2;
}
10、strstr的使⽤和模拟实现
#include <stdio.h>
#include <string.h>
#include <assert.h>
char* my_strstr(const char* str1, const char* str2);
int main()
{char str1[] = "abbbcdef";char str2[] = "sda";char* ret = my_strstr(str1, str2);printf("%s", ret);return 0;
}
char* my_strstr(const char* str1, const char* str2)
{assert(str1);assert(str2);const char* s1;const char* s2;while (*str1) {s1 = str1;s2 = str2;while (*s1!='\0' && *s2 != '\0' && *s1 == *s2){s1++;s2++;}if (*s2 == '\0'){return str1;}str1++;}return NULL;}
11、memcpy使⽤和模拟实现
#include <stdio.h>
#include <assert.h>
void* my_memcpy(void* dest, void* src, int num);
int main()
{int arr1[] = { 1,2,3,4,5,6,7,8,9 };int arr2[] = { 6,7,8,9 };my_memcpy(arr1, arr2, 8);for (int i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++){printf("%d", arr1[i]);}return 0;
}
void* my_memcpy(void* dest, void* src, int num)
{assert(dest);assert(src);void* p = dest;while (num){*(char*)dest = *(char*)src;(char*)dest = (char*)dest + 1;(char*)src = (char*)src + 1;num--;}return p;
}
12、memmove使⽤和模拟实现
#include <stdio.h>
void* my_memmove(const void* dest, const void* src, int num);
int main()
{int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };my_memmove(&arr[2], &arr[3], 2);for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++){printf("%d ", arr[i]);}return 0;
}
void* my_memmove(const void* dest, const void* src, int num)
{void* p = dest;if (dest <= src || dest > ((char*)src + num)){while (num){*(char*)dest = *(char*)src;(char*)dest = (char*)dest + 1;(char*)src = (char*)src + 1;num--;}}else{(char*)dest = (char*)dest + num - 1;(char*)src = (char*)src + num - 1;while (num){*(char*)dest = *(char*)src;(char*)dest = (char*)dest - 1;(char*)src = (char*)src - 1;num--;}}return p;
}