1. strlen
2. strcpy
3. strcat
4. strstr
5. strchr
6. strcmp
7. memcpy
8. memmove
1.strlen有3种方法实现:
a.计数器实现
代码如下:
#include
int my_strlen(const char* str)
{
int count = 0;
while (*str)
{
count++;
str++;
}
return count;
}
int main()
{
int ret = 0;
char* arr = "hello world";
ret = my_strlen(arr);
printf("%d\n", ret);
return 0;
}
b.递归实现
代码如下:
#include "stdio.h"
int my_strlen(const char* arr)
{
if (*arr == '\0')
{
return 0;
}
else
{
return 1 + my_strlen(arr + 1);
}
}
int main()
{
int ret = 0;
char* arr = "hello world";
ret = my_strlen(arr);
printf("%d\n", ret);
return 0;
}
#include "stdio.h"
int my_strlen( char* arr)
{
char* p = arr;
while (*arr)
{
arr++;
}
return arr - p;
}
int main()
{
int ret = 0;
char* arr = "hello world";
ret = my_strlen(arr);
printf("%d\n", ret);
return 0;
}
#include
#include
char* my_strcpy(char* dest, const char*src)
{
char* ret = dest;
assert(dest&&src);
while ((*dest++ = *src++))
{
;
}
return ret;
}
int main()
{
char arr1[] = "abcdef";
char arr2[20];
my_strcpy(arr2, arr1);
printf("%s\n", arr2);
return 0;
}
#include
#include
char* my_strcat(char* dest, const char* src)
{
char* ret = dest;
assert(dest&&src);
while (*dest)
{
dest++;
}
while ((*dest++=*src++))
{
;
}
return ret;
}
int main()
{
char arr1[] = "welcome to bite";
char arr2[50] = "my baby ";
my_strcat(arr2, arr1);
printf("%s\n", arr2);
return 0;
}
#include
#include
char* my_strstr(const char* str1, const char* str2)
{
assert(str2&&str1);
char* p1 = (char*)str1;
char* p2 = (char*)str2;
char *s1 = NULL;
if (*str2 == '\0')
return NULL;
while (*p1)
{
s1 = p1;
p2 = (char*)str2;
while (*s1&&*p2 && (*s1==*p2))
{
s1++;
p2++;
}
if (*p2 == '\0')
{
return p1;
}
p1++;
}
rturn NULL;
}
int main()
{
char arr1[] = "abbbcdef";
char arr2[] = "bbcd";
char* ret=my_strstr(arr1, arr2);
printf("%s\n", ret);
return 0;
}
#include
#include
char* my_strchr(const char* dest, char c)
{
assert(dest!=NULL);
char* p = (char*)dest;
if (*p == '\0')
{
return NULL;
}
while (*p)
{
if (*p==c)
{
return p;
}
p++;
}
}
int main()
{
char arr[] = "abcfcfac";
char c = 'f';
char* ret = my_strchr(arr, c);
printf("%s\n", ret);
return 0;
}
#include
#include
int my_strcmp(const char* dest, const char* src)
{
assert(dest&&src);
while ((*dest == *src) && *dest&&*src)
{
if ((*dest=='\0')||(*src=='\0'))
{
return 0;
}
dest++;
src++;
}
if (*dest>*src)
{
return 1;
}
else
return -1;
}
int main()
{
char arr1[] = "dfgy";
char arr2[] = "gygyy";
int ret =my_strcmp(arr1, arr2);
printf("%d\n", ret);
return 0;
}
运行结果:
7.memcpy
内存拷贝,和strcpy相比,速度更快。
代码如下:
#include
#include
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;
count--;
}
return ret;
}
int main()
{
char arr1[50];
char arr2[] = "abcdef";
my_memcpy(arr1, arr2, 7);
printf("%s\n", arr1);
return 0;
}
运行结果;
memcpy运用中存在的缺陷,即memcpy不能拷贝目的地址(dest)和源地址(src)内存空间有重合的部分,更为确切的说应该是当目的地址大于源地址的时候,不能够有重合部分,否则源地址重合部分数据会发生错误。
比如:
#include
#include
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;
count--;
}
return ret;
}
int main()
{
int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int i = 0;
my_memcpy(arr + 4, arr + 2, 16);
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
运行结果:
显而易见,当我想把数组1 2 3 4 5 6 7 8 9 10中3 4 5 6拷贝到5 6 7 8的位置时,由于出现重叠,所以拷贝失败。
8.memmove
他就能完美的解绝memcpy中的缺陷
代码如下:
#include
#include
void* my_memmove(void* dest, void const * src, size_t count)
{
assert(dest&&src);
char* p1 = (char*)dest;
char* p2 = (char*)src;
if ((p1 > p2) && (p1 < p2 + count))
{
while (count--)
{
*(p1 +count)= *(p2+count);
}
}
else
{
while (count--)
{
*p1++ = *p2++;
}
}
return dest;
}
int main()
{
int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int i = 0;
my_memmove(arr+4, arr+2, 16);
for (i = 0; i < 10;i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}