目录
一、字符串函数
1、strstr函数的模拟实现
char *strstr( const char *string, const char *strCharSet );strstr函数的作用是返回字符串首次出现的字串的地址
函数实现
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<assert.h>
char* my_strstr(const char* str1, const char* str2)
{
assert(str1!=NULL&&str2 != NULL);
const char* p = str1;
const char* p1 = NULL;
const char* p2 = NULL;
if (*str2 =='\0')
return (char*)str1;
while (*p)
{
p1 = p;
p2 = str2;
while (*p1 == *p2)
{
p1++;
p2++;
}
if (*p2 == '\0')
return (char*)p;
p++;
}
return NULL;
}
void main()
{
char str1[] = "ijfsimkgrgi";
char str2[] = "mkg";
printf("%p\n", my_strstr(str1, str2));
printf("%p\n",strstr(str1,str2));
}
2、strlen函数的模拟实现
size_t strlen( const char *string );strlen所作的是一个计数器的工作,它从内存的某个位置(可以是字符串开头,中间某个位置,甚至是某个不确定的内存区域)开始扫描,直到碰到第一个字符串结束符'\0'为止,然后返回计数器值(长度不包含'\0')。
函数实现
#include<stdio.h>
#include<string.h>
#include<assert.h>
size_t my_strlen(const char *a)
{
assert(a != NULL);
int sum = 0;
while (*a != '\0')
{
sum++;
a++;
}
return sum++;
}
void main()
{
const char* a = "djncwjsnx";
printf("%u", my_strlen(a));
}
3、strcpy函数的模拟实现
char *strcpy( char *strDestination, const char *strSource );strcpy把含有'\0'结束符的字符串复制到另一个地址空间,返回值的类型为char*
函数实现
#include<stdio.h>
#include<string.h>
#include<assert.h>
char* my_strcpy(char* str1, char* str2)
{
assert(str1 != NULL && str2 != NULL);
char* a = str1;
char* b = str2;
while (*a != '\0')
{
*a = *b;
a++;
b++;
}
*a = '\0';
return str1;
}
void main()
{
char str1[] = "nvjndenj";
char str2[] = "bfhcdhc";
printf("%s\n", my_strcpy(str1, str2));
}
4、strcmp函数的模拟实现
int strcmp( const char *string1, const char *string2 );strcmp函数是string compare(字符串比较)的缩写,用于比较两个字符串并根据比较结果返回整数。基本形式为strcmp(str1,str2),若str1=str2,则返回零;若str1<str2,则返回负数;若str1>str2,则返回正数。
函数实现
#include<stdio.h>
#include<string.h>
#include<assert.h>
int my_strcmp(char* str1,char* str2)
{
assert(str1 != NULL && str2 != NULL);
while (*str1 != '\0' && *str2 != '\0')
{
if (*str1 > *str2)
return 1;
else if (*str1 > *str2)
return -1;
str1++;
str2++;
}
if (*str1 == '\0')
return -1;
else if (*str2 == '\0')
return 1;
else
return 0;
}
void main()
{
char str1[] = "nvjndenj";
char str2[] = "bfhcdhc";
int value=my_strcmp(str1, str2);
printf("%d", value);
}
5、strcat函数的模拟实现
extern char *strcat(char *dest, const char *src);把src所指向的字符串(包括“\0”)复制到dest所指向的字符串后面(删除*dest原来末尾的“\0”)。要保证*dest足够长,以容纳被复制进来的*src。*src中原有的字符不变。返回指向dest的指针。
函数实现
#include<stdio.h>
#include<string.h>
#include<assert.h>
char* my_strcat(char* str1,char* str2)
{
assert(str1 != NULL && str2 != NULL);
char* a = str1;
char* b = str2;
while (*a != '\0')
{
a++;
}
while (*b != '\0')
{
*a = *b;
a++;
b++;
}
*a = '\0';
return str1;
}
void main()
{
char str1[100] = "nvjndenj";
char str2[] = "bfhcdhc";
printf("%s\n", my_strcat(str1, str2));
}
二、内存函数
1、memcpy函数的模拟实现
void *memcpy(void *destin, void *source, unsigned n);函数的功能是从源内存地址的起始位置开始拷贝若干个字节到目标内存地址中,即从源source中拷贝n个字节到目标destin中。
函数实现
void* my_memcpy(void* dest, const void* src, size_t num)
{
assert(dest!= NULL && src!= NULL);
char* res = dest;
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return res;
}
注意:memcpy 函数适用于拷贝内存不重叠部分
2、memmove函数的模拟实现
void *memmove( void *dest, const void *src, size_t count );
memmove 函数从 src 复制字符的字节数到 dest,如果源区域和目标区域的某些区域重叠,memmove 确保在重叠区域的原始源字节被复制后再被覆盖。
函数实现
void* my_memmove(void* dest, const void* src, size_t num)
{
assert(dest!= NULL && src!= NULL);
void* res = dest;
if (dest<src)
{ //从前往后拷贝
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else
{ //从后向前拷贝
while (num--)
{
*((char*)dest + num) = *((char*)src + num);
}
}
return res;
}
3、memcpy函数的模拟实现
int memcmp( const void *buf1, const void *buf2, size_t count );memcmp 函数比较 buf1 和 buf2 的 count 字节,并返回指示它们之间关系的值,返回值和上面的 strcmp 一样,buf1 大于 buf2,返回1,等于返回0,小于返回 -1。
函数实现
int my_memcmp(const void* buf1, const void* buf2, size_t count)
{
assert(buf1!=NULL&& buf2!=NULL);
while (*((char*)buf1) == *((char*)buf2) && count--)
{
((char*)buf1)++;
((char*)buf2)++;
}
return (*(char*)buf1) - (*(char*)buf2);
}