一、模拟strcpy
//模拟strcpy
#include<stdio.h>
#include<stdlib.h>
#include <assert.h>
char *my_strcpy(char *dest, const char *src) {
assert(dest != NULL);
assert(src != NULL);
char *ret = dest;
while (*dest&&*src){
*dest = *src;
dest++;
src++;
}
return ret;
}
int main() {
char a[] = "abcd";
char b[] = "123";
char *ret = my_strcpy(a, b);
puts(ret);
system("pause");
return 0;
}
二、模拟strcat
//模拟strcat
#include<stdio.h>
#include<stdlib.h>
#include <assert.h>
char *my_strcat(char *dest, const char*src) {
assert(dest != NULL);
assert(src != NULL);
char *ret = dest;
while (*dest){
dest++;
}
while (*src){
*dest = *src;
dest++;
src++;
}
*dest = '\0';
return ret;
}
int main() {
char a[] = "abcd";
char b[] = "123456";
char *ret = my_strcat(a, b);
puts(ret);
system("pause");
return 0;
}
三、模拟strstr
//模拟strstr
#include<stdio.h>
#include<stdlib.h>
#include <assert.h>
char *my_strstr(const char *str1, const char *str2) {
assert(str1 != NULL);
assert(str2 != NULL);
char *tstr1 = str1;
char *tstr2 = str2;
while (*tstr1){
char *flag = tstr1;
while (*tstr1 == *tstr2 && *tstr1){
tstr1++;
tstr2++;
}
if (!*tstr2){
return flag;
}
if (!*tstr1){
return NULL;
}
tstr1++;
}
return NULL;
}
int main() {
char a[] = "abcd123";
char b[] = "bc";
char *ret = my_strstr(a, b);
puts(ret);
system("pause");
return 0;
}
四、模拟strchr
//模拟strchr
#include<stdio.h>
#include<stdlib.h>
char *my_strchr(const char *str, char c) {
if (*str==NULL){
return NULL;
}
char *p = (char *)str;
while (*p){
if (*p==c){
return p;
}
p++;
}
return NULL;
}
int main() {
char str[] = "1234567";
char c = '3';
char *ret = my_strchr(str, c);
printf("%p\n", str);
printf("%p\n", ret);
system("pause");
return 0;
}
五、模拟strcmp
//模拟strcmp
#include<stdio.h>
#include<stdlib.h>
#include <assert.h>
char *my_strcmp(const char *dest, const char *src) {
assert(dest != NULL);
assert(src != NULL);
while (*dest == *src) {
if (*dest=='\0'){
return 0;
}
dest++;
src++;
}
return *dest - *src;
}
int main() {
char str1[] = "abcdef";
char str2[] = "abcadf";
char ret = my_strcmp(str1, str2);
printf("%d\n", ret);
system("pause");
return 0;
}
六、模拟memcpy
//模拟memcpy
#include<stdio.h>
#include<stdlib.h>
void *my_memcpy(void *dest, const void *src, size_t n) {
char *tmp = (char *)dest;
char *s_src = (char *)src;
while (n--){
*tmp++ = *s_src++;
}
return dest;
}
int main() {
char str1[] = "123456789";
my_memcpy(str1, str1+2, 4); //能正确显示
//my_memcpy(str1 + 2, str1, 4); //内存重叠
printf("%s\n", str1);
system("pause");
return 0;
}
七、模拟memmove
//模拟memmove
#include<stdio.h>
#include<stdlib.h>
void *my_memmove(void *dest, const void *src, size_t n) {
char *s_dest = (char *)dest;
char *s_src = (char *)src;
if (s_dest>s_src&&s_src+n>s_dest){
s_dest = s_dest + n - 1;
s_src = s_src + n - 1;
while (n--){
*s_dest-- = *s_src--;
}
}
else
{
while (n--){
*s_dest++ = *s_src++;
}
}
return dest;
}
int main() {
char str1[] = "123456789";
//my_memmove(str1, str1 + 2, 4); //能正确显示
my_memmove(str1 + 2, str1, 4); //memcpy内存重叠的情况也能正确显示
printf("%s\n", str1);
system("pause");
return 0;
}