1、strstr
strstr函数有两个版本:
const char * strstr ( const char * str1, const char * str2 );
char * strstr ( char * str1, const char * str2 );
(1) 朴素的实现方式
遍历两个字符串,在str1中逐个匹配str2,时间复杂度O(nm).
(2) KMP算法
strstr的两种实现参考文章:
KMP(Knuth-Morris-Pratt)算法
2、strlen
以下两种实现类似,后一种没有借助局部length变量。
size_t strlen1(const char *str) {
assert(str != NULL);
unsigned int length = 0;
while ((*str++) != '\0')
++length;
return length;
}
size_t strlen2(const char *str) {
assert(str != NULL);
const char *end = str;
while (*end++) ;
return ((int)(end - str - 1));
}
3、strcat strncat
注意几点:
a. 给源字符加上const属性;
b. 给源地址和目的地址加非零断言;
c. 为了实现链式操作,将目的地址返回,而不是返回void;
d. 考虑源目的区域有重叠的情况;
e. 一定要保证追加操作完后,目的地址最后以空字符'\0‘结尾。
</pre><pre name="code" class="cpp">char *strcat1(char *destination, const char *source) {
assert (destination != NULL && source != NULL);
char *cp = destination;
while (*cp)
++cp;
while (*cp++ = *source++) ;
return destination;
}
char* strncat1(char *destination, const char *source, size_t count) {
assert (destination != NULL && source != NULL);
char *cp = destination;
while (*cp)
++cp;
while (count-- && *source != '\0')
*cp++ = *source++;
*cp = '\0';
return destination;
}
4、strcmp strncmp
注意:下面字符做减法时,要强制类型转换,将char转换为unsigned char,因为strcmp函数是按照ASCII码进行比较的,而ASCII码的范围是0 ~ 255,char的范围是-127 ~ 127,所以当输入为负数时会返回错误。
int strcmp1(const char *str1, const char *str2) {
assert(str1 != NULL && str2 != NULL);
int result = 0;
while ( !(result = *(unsigned char*)str1 - *(unsigned char*)str2) && *str2) {
++str1;
++str2;
}
if (result < 0)
return -1;
else if (result > 0)
return 1;
return result;
}
int strncmp1(const char *str1, const char *str2, size_t count) {
assert(str1 != NULL && str2 != NULL);
int result = 0;
//下一行必须将count--写在前边,否则count等于0时还会计算一个ret
while (count-- && (!(result = *(unsigned char*)str1 - *(unsigned char*)str2)) && *str2) {
++str1;
++str2;
}
if (result < 0)
return -1;
else if (result > 0)
return 1;
return result;
}
5、strcpy strncpy
注意几点:
a. 给源字符加上const属性;
b. 给源地址和目的地址加非零断言;
c. 为了实现链式操作,将目的地址返回,而不是返回void;
d. 考虑源目的区域有重叠的情况;
e. 一定要保证复制完后,目的地址最后以空字符'\0‘结尾。
char *strcpy(char *destination, const char *source) {
assert(destination != NULL && source != NULL);
if (destination == source)
return destination;
char *cp = destination;
while ((*cp++ = *source++) != '\0')
;
return destination;
}
char *strncpy1(char *destination, const char *source, size_t count) {
assert(destination != NULL && source != NULL);
if (destination == source)
return destination;
char *cp = destination;
while (count-- && *source != '\0')
*cp++ = *source++;
*cp = '\0';
return destination;
}
6、strpbrk
有两个版本:
const char * strpbrk ( const char * str1, const char * str2 );
char * strpbrk ( char * str1, const char * str2 );
/*
Returns a pointer to the first occurrence in str1 of any of the
characters that are part of str2, or a null pointer if there are no matches.
The search does not include the terminating null-characters
该函数也是两个版本:const和非const版本
*/
char *strpbrk1(char *str1, const char *str2) {
assert((str1 != NULL) && (str2 != NULL));
const char *s;
while (*str1 != '\0') {
s = str2;
while (*s != '\0'){
if (*str1 == *s)
return str1;
++ s;
}
++ str1;
}
return NULL;
}
7、memcpy
该函数不检查source结尾的null字符,仅仅拷贝count个字节。为了避免溢出,destination和source指针所指的数组必须最少有count个字节,而且两个区域不能重叠。
如果区域有重叠,那么要使用memmove这个更安全的方式。
void *memcpy1(void *destination, const void *source, size_t count) {
assert (destination != NULL && source != NULL);
void *address = destination;
while (count--) {
*(char*)destination = *(char*)source;
destination = (char *)destination + 1;
source = (char *)source + 1;
}
return address;
}
8、memmove
和memcpy函数一样,该函数也不会检查source末尾的空字符null,仅仅拷贝count个字节;为了避免溢出,destination和source指针所指的数组必须最少有count个字节。但是该函数允许源和目的区域重叠。
void *memmove1(void *destination, const void *source, size_t count) {
assert (destination != NULL && source != NULL);
char *pdest = (char*)destination;
char *psrc = (char*)source;
//pdest在psrc后面,且两者距离小于count,从尾部开始移动,
//其他情况从头部开始移动
if ((pdest > psrc) && (pdest - psrc < count)) {
while (count--)
*(pdest + count) = *(psrc + count);
}
else {
while (count--)
*pdest++ = *psrc++;
}
return destination;
}
9、memset
void *memset1(void *str, int value, size_t count) {
if (str == NULL)
return NULL;
void *p = str;
while (count--) {
*(char*)p = (char)value;
p = (char *)p + 1;
}
return str;
}
10、strchr memchr
memchr函数功能:查找在num字节内,value(解释为unsigned char)第一次出现的位置,返回指向它的指针。
两个版本:
const void * memchr ( const void * ptr, int value, size_t num );
void * memchr ( void * ptr, int value, size_t num );
void *memchr1(void *str, int value, size_t count) {
if (str == NULL)
return NULL;
while (count--) {
if (*(char*)str == value)
return (void*)str;
str = (char*)str + 1;
}
return NULL;
}
strchr也有两个版本:
const char * strchr ( const char * str, int character );
char * strchr ( char * str, int character );
//查找字符串s中首次出现字符c的位置
char *strchr1(char *str, int c) {
assert(str != NULL);
for (; *str != (char)c; ++ str)
if (*str == '\0')
return NULL;
return str;
}
参考:程序员编程艺术:第四章、现场编写类似strstr/strcpy/strpbrk的函数