下面将分别模拟实现memcpy,memmove,strstr,strcat,strcmp,strcpy,strlen
模拟实现memcpy
#include <stdio.h>
#include <string.h>
#include <assert.h>
void* my_memcpy(void* dest, const void* src, size_t count)
{
assert(dest && src);
void* ret = dest;
while (count--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return ret;
}
int main()
{
int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[20] = { 2,3,4,5,6,7,8,9,10,11 };
int i = 0;
my_memcpy(arr1, arr2, 16);
for (i = 0; i < 10; i++)
{
printf("%d ", arr1[i]);
}
return 0;
}
模拟实现memmove
#include <stdio.h>
#include <string.h>
#include <assert.h>
void* my_memmove(void* dest, const void* src, size_t count)
{
assert(dest && src);
void* ret = dest;
assert(dest && src);
if (dest < src)
{
while (count--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else
{
while (count--)
{
*((char*)dest + count) = *((char*)src + count);
}
}
return ret;
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
my_memmove(arr, arr + 2, 16);
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
}
模拟实现strstr
#include <stdio.h>
#include <assert.h>
char* my_strstr(const char* str1, const char* str2)
{
assert(str1 && str2);
const char* s1;
const char* s2;
const char* sp = str1;
if (*str2 == '\0')
return str1;
while (*sp)
{
s1 = sp;
s2 = str2;
while (*s1 && *s2 && *s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
return sp;
sp++;
}
return NULL;
}
int main()
{
char str1[] = "Do the right thing at the right time";
char str2[] = "right";
char* ret = my_strstr(str1, str2);
if (NULL == ret)
printf("找不到\n");
else
printf("%s\n", ret);
return 0;
}
模拟实现strcat
#include <stdio.h>
#include <assert.h>
char* my_strcat(char* dest, const char* src)
{
assert(dest && src);
char* ret = dest;
while (*dest)
{
dest++;
}
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char str1[20] = "I am ";
char str2[] = "zhao";
my_strcat(str1, str2);
printf("%s\n", str1);
return 0;
}
模拟实现strcmp
#include <stdio.h>
#include <assert.h>
int my_strcmp(const char* str1, const char* str2)
{
assert(str1, str2);
while (*str1 == *str2)
{
if (*str1 == '\0')
return 0;
str1++;
str2++;
}
return *str1 - *str2;
}
int main()
{
char str1[] = "abcdeg";
char str2[] = "abcdef";
int ret = my_strcmp(str1, str2);
if (0 > ret)
printf("<\n");
else if (0 < ret)
printf(">\n");
else
printf("=\n");
return 0;
}
模拟实现strcpy
#include <stdio.h>
#include <assert.h>
char* my_strcpy(char* dest, const char* src)
{
assert(dest && src);
char* ret = dest;
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char str1[] = "at the not same";
char str2[] = "at the same!!!";
char* ret = my_strcpy(str1, str2);
printf("%s\n", ret);
perror("test:");
return 0;
}
模拟实现strlen
#include <stdio.h>
#include <assert.h>
#include <string.h>
size_t my_strlen(const char* str)
{
assert(str);
size_t count = 0;
while (*str)
{
str++;
count++;
}
return count;
}
size_t my_strlen(const char* str)
{
assert(str);
if (*str)
{
str++;
return 1 + my_strlen(str);
}
else
return 0;
}
size_t my_strlen(const char* str)
{
char* p = str;
while (*str)
{
str++;
}
return str - p;
}
int main()
{
char str[] = "Today is a good day !";
size_t len = my_strlen(str);
printf("%d\n", len);
perror("test:");
return 0;
}
|