字节拷贝函数:
void * memcpy1(void *dest, const void *src, size_t n)
{
assert(dest!=NULL&&src!=NULL); //有效性检测
if(dest == src)
return dest;
else if(dest < src)
{
const char *c_src = static_cast<const char *>(src);
char *c_dest = static_cast<char *>(dest);
while (n--) *c_dest++ = *c_src++;
}
else
{
const char *c_src = static_cast<const char *>(src) + n - 1;
char *c_dest = static_cast<char *>(dest) + n - 1;
while (n--) *c_dest-- = *c_src--;
}
return dest;
}
字符串比较函数:
char *strcpy(char *des, const char *src)
{
assert(des != NULL && src != NULL);
char *pStr = des;
int strcmp(const char *des, const char *src)
{
assert(des != NULL && src != NULL);
int strlen(const char *pStr)
{
assert(pStr != NULL);
int len = 0;
while (*pStr++ != '\0')
len++;
return len;
char *strcat(char *des, const char *src)
{
assert(*des != NULL && *src != NULL);
char *pStr = des;
while (*des != '\0')
des++;
char *myStrstr(const char *des, const char *src)
{
assert(des != NULL && src != NULL);
char *d = des;
char *s = src;
for (; d != '\0'; des++)
{
for (d = des, s = src; (s != '\0') && (*d == *s); d++, s++)
NULL;
if (*s == '\0')
return des;
}
return NULL;
}
void * memcpy1(void *dest, const void *src, size_t n)
{
assert(dest!=NULL&&src!=NULL); //有效性检测
if(dest == src)
return dest;
else if(dest < src)
{
const char *c_src = static_cast<const char *>(src);
char *c_dest = static_cast<char *>(dest);
while (n--) *c_dest++ = *c_src++;
}
else
{
const char *c_src = static_cast<const char *>(src) + n - 1;
char *c_dest = static_cast<char *>(dest) + n - 1;
while (n--) *c_dest-- = *c_src--;
}
return dest;
}
字符串比较函数:
char *strcpy(char *des, const char *src)
{
assert(des != NULL && src != NULL);
char *pStr = des;
while( (*des++ = *src++) != '\0');
}
int strcmp(const char *des, const char *src)
{
assert(des != NULL && src != NULL);
for (;*des && *src && *des == *src; des++, src++);
}
int strlen(const char *pStr)
{
assert(pStr != NULL);
int len = 0;
while (*pStr++ != '\0')
len++;
return len;
}
char *strcat(char *des, const char *src)
{
assert(*des != NULL && *src != NULL);
char *pStr = des;
while (*des != '\0')
des++;
while ((*des++ = *src++) != '\0');
return pStr;
}
char *myStrstr(const char *des, const char *src)
{
assert(des != NULL && src != NULL);
char *d = des;
char *s = src;
for (; d != '\0'; des++)
{
for (d = des, s = src; (s != '\0') && (*d == *s); d++, s++)
NULL;
if (*s == '\0')
return des;
}
return NULL;
}