2023年12月11日发(作者:玉珠集团)
常用字符串源代码
1、strstr
strstr函数有两个版本:
[cpp]
1.
2.
const
char * strstr (
const
char * str1,
const
char * str2 );
char * strstr ( char * str1,
const
char * str2 );
(1) 朴素的实现方式
遍历两个字符串,在str1中逐个匹配str2,时间复杂度O(nm).
(2) KMP算法
strstr的两种实现参考文章:
2、strlen
以下两种实现类似,后一种没有借助局部length变量。
[cpp]
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
size_t strlen1(
const
char *str) {
asrt(str != NULL);
unsigned int length = 0;
while
((*str++) != '0')
++length;
return
length;
}
size_t strlen2(
const
char *str) {
asrt(str != NULL);
const
char *end = str;
while
(*end++) ;
return
((int)(end - str - 1));
}
3、strcat strncat
注意几点:注意几点
a. 给源字符加上const属性;
b. 给源地址和目的地址加非零断言;
c. 为了实现链式操作,将目的地址返回,而不是返回void;
d. 考虑源目的区域有重叠的情况;
e. 一定要保证追加操作完后,目的地址最后以空字符'0‘结尾。
[cpp]
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
class
="cpp">char *strcat1(char *destination,
const
char *source) {
asrt (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) {
asrt (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,所以当输入为负数时会返回错误。
[cpp]
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
int strcmp1(
const
char *str1,
const
char *str2) {
asrt(str1 != NULL && str2 != NULL);
int result = 0;
while
( !(result = *(unsigned char*)str1 - *(unsigned char*)str2) && *str2) {
++str1;
++str2;
}
if
(result < 0)
return
-1;
el
if
(result > 0)
return
1;
return
result;
}
int strncmp1(
const
char *str1,
const
char *str2, size_t count) {
asrt(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;
el
if
(result > 0)
return
1;
return
result;
}
5、strcpy strncpy
注意几点:注意几点
a. 给源字符加上const属性;
b. 给源地址和目的地址加非零断言;
c. 为了实现链式操作,将目的地址返回,而不是返回void;
d. 考虑源目的区域有重叠的情况;
e. 一定要保证复制完后,目的地址最后以空字符'0‘结尾。[cpp]
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
char *strcpy(char *destination,
const char *source) {
asrt(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) {
asrt(destination != NULL && source != NULL);
if (destination == source)
return destination;
char *cp = destination;
while (count-- && *source != '0')
*cp++ = *source++;
*cp = '0';
return destination;
}
6、strpbrk有两个版本:[cpp]
1.
2.
const char * strpbrk (
const char * str1,
const char * str2 );
char * strpbrk ( char * str1,
const char * str2 );
[cpp]
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
/*
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 arch does not include the terminating null-characters
该函数也是两个版本:const和非const版本
*/
char *strpbrk1(char *str1,
const
char *str2) {
asrt((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这个更安全的方式。
[cpp]
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
void
*memcpy1(
void
*destination,
const
void
*source, size_t count) {
asrt (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个字节。但是该函数允许源和目的该函数允许源和目的区域重叠。
[cpp]
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
void
*memmove1(
void
*destination,
const
void
*source, size_t count) {
asrt (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);
}
el
{
while
(count--)
*pdest++ = *psrc++;
}
return
destination;
}
9、memt
[cpp]
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
void
*memt1(
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)第一次出现的位置,返回指向它的指针。
两个版本:
[cpp]
1.
2.
const
void
* memchr (
const
void
* ptr, int value, size_t num );
void
* memchr (
void
* ptr, int value, size_t num );
[cpp]
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
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也有两个版本:
[cpp]
1.
2.
const
char * strchr (
const
char * str, int character );
char * strchr ( char * str, int character );
[cpp]
1.
2.
3.
4.
5.
6.
7.
8.
//查找字符串s中首次出现字符c的位置
char *strchr1(char *str, int c) {
asrt(str != NULL);
for
(; *str != (char)c; ++ str)
if
(*str == '0')
return
NULL;
return
str;
}
-
![]()
本文发布于:2023-12-11 18:57:43,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/zhishi/a/1702292267243245.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:常用字符串源代码.doc
本文 PDF 下载地址:常用字符串源代码.pdf
留言与评论(共有 0 条评论) |