1.实现strcpy
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
char *my_strcpy(char *str1, const char *str2)
{
assert(str1);
assert(str2);
char *str3=str1;
while ((*str3++) = (*str2++))
;
return str1;
}
int main()
{
char str1[20] = "123456";
char *str2 = "abcde";
my_strcpy(str1, str2);
printf("%s\n", str1);
return 0;
}
运行结果:
2.实现strcat
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
char *my_strcat(char *str1, const char *str2)
{
assert(str1);
assert(str2);
char *dest = str1;
while (*++dest);
while (*dest++ = *str2++);
return str1;
}
int main()
{
char str1[20] = "123456";
char *str2 = "abcdef";
my_strcat(str1, str2);
printf("%s\n", str1);
return 0;
}
运行结果:
3.实现strstr
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
#include<string.h>
char *my_strstr( char *str, char *substr)
{
assert(str);
assert(substr);
char *start = str;
char *end = str + strlen(str) - strlen(substr) + 1;
while (start < end)
{
char *str_start = start;
char *sub_start = substr;
while (*sub_start)
{
if (*str_start == *sub_start)
{
str_start++, sub_start++;
}
else
{
break;
}
}
if (*sub_start == '\0')
{
return start;
}
start++;
}
return NULL;
}
int main()
{
char dest[20] = "abcdefg123456";
char *ret = my_strstr(dest, "defg");
printf("%s\n", ret);
return 0;
}
运行结果:
没有找到返回null
4.实现strchr
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
char *my_strchr(char *des, char ch)
{
assert(des);
assert(ch);
while (*des)
{
if (*des == ch)
{
return des;
}
des++;
}
return NULL;
}
int main()
{
char des[20] = "abcdef";
char *ret=my_strchr(des, 'd');
printf("%s\n", ret);
return 0;
}
运行结果:
没有找到返回null
5.实现strcmp
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
int my_strcmp(const char *msg1, const char *msg2)
{
assert(msg1);
assert(msg2);
const char *p = msg1;
const char *q = msg2;
while (*p != '\0'&&*q != '\0')
{
if (*p == *q)
{
p++, q++;
}
else if (*p > *q)
{
return 1;
}
else
{
return -1;
}
}
if (*p != '\0')
{
return 1;
}
if (*q != '\0')
{
return -1;
}
return 0;
}
int main()
{
const char *msg1 = "abcdef";
const char *msg2 = "abcdeg";
int ret = my_strcmp(msg1, msg2);
printf("ret = %d\n", ret);
return 0;
}
运行结果:
6.实现memcpy
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
void *my_memcpy(void *dst, const void* src, int count)
{
assert(dst);
assert(src);
char *dst_p = (char *)dst;
const char *src_p = (const char*)src;
while (count--)
{
*dst_p = *src_p;
dst_p++;
src_p++;
}
return dst;
}
int main()
{
int a[] = { 1, 2, 3, 4 };
int b[4];
my_memcpy(b, a, sizeof(a));
for (int i = 0; i < 4; i++)
printf("%d ", b[i]);
printf("\n");
return 0;
}
运行结果:
7.实现memmove
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<assert.h>
void *my_memmove(void *dst, const void *src,int count)
{
assert(dst);
assert(src);
char *p = (char *)dst;
const char *q = (const char *)src;
if ((p > q) && (p < q + count))
{
p = p + count - 1;
q = q + count - 1;
while (count--)
{
*p = *q;
p--, q--;
}
}
else
{
while (count--)
{
*p = *q;
p++, q++;
}
}
return dst;
}
int main()
{
char arr[20] = "abcdefghijklmn";
my_memmove(arr+1, arr , strlen(arr));
printf("%s\n", arr);
return 0;
}
运行结果: