<<字符串高级截取和统计>>一文的C语言实现
突然看到首页出现两篇题目极为相似的文章,一看,居然还是不同作者,看了幽梦新影兄的执行结果,突然想起自己好像也有用C语言写过类似代码。(纯属凑热闹了)
《<<字符串高级截取和统计>>一文的看法与正则表达式的实现》
《字符串高级截取和统计》(补充一下,尽然又发现一篇~)
不过不知道为什么遇到这样的功能大家就一定要想到“正则实现”呢?曾经参加微软的一个第三方培训的时候得知正则表达式的效率相当的低下,大部分的性能瓶颈均出于此,不过那个培训是高性能培训。
但值得肯定的是这个正则表达式绝对是一个优秀的实践。很多纷繁复杂的查找匹配规则通过正则表达式立刻就能迅速求解了。
下面这个C语言程序是我以前写着玩的程序,刚才突然想到了,就贴出来一同完善这个解决方案。不过不难看出用传统的字符构成字符串的思路,要写完这个类似的功能,工作量还是相当巨大的。但是用正则表达式则能够让代码简洁优美。至于效率,我个人认为其实完全可以忽略不计。
不过我这个代码的实现好像有附加了其它的一些功能,不过没关系,只增不减。
1 /*
2 * textsearch.h
3 *
4 * Created on:
5 * Author: Volnet
6 * Website: http://volnet.cnblogs.com
7 *
8 */
9
10 #ifndef TEXTSEARCH_H_
11 #define TEXTSEARCH_H_
12
13 #include <stdarg.h>
14
15 #ifndef INDEX_T_DEFINDED
16 typedef int index_t;
17 #define INDEX_T_DEFINDED
18 #endif
19
20 #ifndef COUNT_T_DEFINDED
21 typedef unsigned int count_t;
22 #define COUNT_T_DEFINDED
23 #endif
24
25 /*
26 * @return
27 * offset: the number of chars that
28 * from you found to the end of the word
29 * after you execute the search_foreach function.
30 * e.g.
31 * if you replace("abcdefgh", "de", "123");//abc45fgh;
32 * you had set offset as 3,
33 * which will let the pointer at the next char 'f';
34 * */
35 typedef void (*search_foreach)(const char *text,
36 const char *found, index_t lengthOfFound,
37 index_t *offset,
38 va_list *paras);
39 typedef void (*search_global)(va_list *paras, void *ret);
40 #define UNFINDED (index_t)-1
41 #define MAX_INDICES 1000;
42
43 /*declartion of functions*/
44
45 /*function :To find the first char(c) in text
46 * @paras
47 * text :a pointer to the first char of the text to search
48 * c :the char to find.
49 * out_indices :(return) an array takes the indices of the tofind in the text.
50 * (value) if(out_indices == NULL), it without any exceptions.
51 * tofind :the word to find in the text.
52 * @return :the index of the char in the text.It will be 'UNFINDED' as no find.
53 * */
54 index_t indexof(const char *text, char c);
55 count_t search_c(const char *text, const char c, index_t *out_indices);
56 count_t search_s(const char *text, const char *tofind, index_t *out_indices);
57 count_t search_s_foreach(
58 /*the full text to search.*/
59 const char *text,
60 /*the word to find in the text.*/
61 const char *tofind,
62 /*(return) an array takes the indices of the tofind in the text.
63 * (value) if(out_indices == NULL), it without any exceptions.
64 * */
65 index_t *out_indices,
66 /*a function to gfunc(
) for the text.
67 * it will execute at the front of all.
68 * */
69 search_global gfunc,
70 /*the number of gfunc's parameters. */
71 const size_t gfunc_paras_count,
72 /*a function to func(
) for each of
73 * the word search_s from text.
74 * */
75 search_foreach func,
76 /*if gfunc!=NULL, the first parameter is the {char *ret; }
77 * ret is the parameter for return.
78 * */
79
80 );
81 /*
82 * function :replace the word 's1' to 's2' in text.
83 * @return :
84 * ret: an array who has enough space to load the result.
85 * text != ret
86 * */
87 void replace(const char *text, const char *s1, const char *s2, char ret[]);
88 /* function :remove the word 's' in text.
89 * @return :
90 * ret: an array who has enough space to load the result.
91 * text != ret
92 * */
93 void to_remove(char *text, const char *s, char ret[]);
94
95 #endif /* TEXTSEARCH_H_ */
96
2 * textsearch.h
3 *
4 * Created on:
5 * Author: Volnet
6 * Website: http://volnet.cnblogs.com
7 *
8 */
9
10 #ifndef TEXTSEARCH_H_
11 #define TEXTSEARCH_H_
12
13 #include <stdarg.h>
14
15 #ifndef INDEX_T_DEFINDED
16 typedef int index_t;
17 #define INDEX_T_DEFINDED
18 #endif
19
20 #ifndef COUNT_T_DEFINDED
21 typedef unsigned int count_t;
22 #define COUNT_T_DEFINDED
23 #endif
24
25 /*
26 * @return
27 * offset: the number of chars that
28 * from you found to the end of the word
29 * after you execute the search_foreach function.
30 * e.g.
31 * if you replace("abcdefgh", "de", "123");//abc45fgh;
32 * you had set offset as 3,
33 * which will let the pointer at the next char 'f';
34 * */
35 typedef void (*search_foreach)(const char *text,
36 const char *found, index_t lengthOfFound,
37 index_t *offset,
38 va_list *paras);
39 typedef void (*search_global)(va_list *paras, void *ret);
40 #define UNFINDED (index_t)-1
41 #define MAX_INDICES 1000;
42
43 /*declartion of functions*/
44
45 /*function :To find the first char(c) in text
46 * @paras
47 * text :a pointer to the first char of the text to search
48 * c :the char to find.
49 * out_indices :(return) an array takes the indices of the tofind in the text.
50 * (value) if(out_indices == NULL), it without any exceptions.
51 * tofind :the word to find in the text.
52 * @return :the index of the char in the text.It will be 'UNFINDED' as no find.
53 * */
54 index_t indexof(const char *text, char c);
55 count_t search_c(const char *text, const char c, index_t *out_indices);
56 count_t search_s(const char *text, const char *tofind, index_t *out_indices);
57 count_t search_s_foreach(
58 /*the full text to search.*/
59 const char *text,
60 /*the word to find in the text.*/
61 const char *tofind,
62 /*(return) an array takes the indices of the tofind in the text.
63 * (value) if(out_indices == NULL), it without any exceptions.
64 * */
65 index_t *out_indices,
66 /*a function to gfunc(

67 * it will execute at the front of all.
68 * */
69 search_global gfunc,
70 /*the number of gfunc's parameters. */
71 const size_t gfunc_paras_count,
72 /*a function to func(

73 * the word search_s from text.
74 * */
75 search_foreach func,
76 /*if gfunc!=NULL, the first parameter is the {char *ret; }
77 * ret is the parameter for return.
78 * */
79

80 );
81 /*
82 * function :replace the word 's1' to 's2' in text.
83 * @return :
84 * ret: an array who has enough space to load the result.
85 * text != ret
86 * */
87 void replace(const char *text, const char *s1, const char *s2, char ret[]);
88 /* function :remove the word 's' in text.
89 * @return :
90 * ret: an array who has enough space to load the result.
91 * text != ret
92 * */
93 void to_remove(char *text, const char *s, char ret[]);
94
95 #endif /* TEXTSEARCH_H_ */
96

下面是调用代码:
1
/*
2
* test.c
3
*
4
* Created on:
5
* Author: Volnet
6
* Website: http://volnet.cnblogs.com
7
*
8
*/
9
10
#include <stdio.h>
11
#include <stdlib.h>
12
#include "../header/textsearch.h"
13
14
int main(void){
15
char *s = "this is a word, who contains \"wh\"!";
16
char c = 'w';
17
18
index_t indices_1[100];
19
index_t *indices1 = indices_1;
20
count_t count1;
21
22
char *tofind = "wh";
23
char *toremove = "onson";
24
char *fromreplace = "wh";
25
char *toreplace = "jonson";
26
index_t indices_2[100];
27
index_t *indices2;
28
count_t count2;
29
char ts[1000];
30
char rs[1000];
31
32
/*e.g:*/
33
/*index_t indexof(char *text, char c);*/
34
printf("the first index of %c is %d.\n",c,indexof(s,c));
35
36
/*count_t search(char *text, char c, index_t *out_indices)*/
37
count1 = search_c(s,c,indices1);
38
printf("the '%c' in the text is in : ",c);
39
while(count1--){
40
printf("%d ",*indices1++);
41
}
42
printf(".\n");
43
44
/*count_t search_s(char *text, const char *tofind, index_t *out_indices)*/
45
indices2 = indices_2;
46
count2 = search_s(s,tofind,indices2);
47
printf("the \"%s\" in the text is in : ",tofind);
48
while(count2--){
49
printf("%d ",*indices2++);
50
}
51
printf(".\n");
52
53
printf("the word is:\n\t%s\n",s);
54
replace(s, fromreplace, toreplace,ts);
55
printf("the word has be changed as (\"%s\"->\"%s\") :\n\t%s\n",fromreplace, toreplace, ts);
56
57
to_remove(ts,toremove,rs);
58
printf("the word remove (remove \"%s\") as : \n\t%s\n",toremove,rs);
59
60
return EXIT_SUCCESS;
61
}
62

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

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

一条正则表达式可以搞定的东西,我们需要写这么多的代码才能够实现,相形见拙就此体现。
下面是输出结果:
the first index of w is 10.
the 'w' in the text is in : 10 16 30 .
the "wh" in the text is in : 16 30 .
the word is:
this is a word, who contains "wh"!
the word has be changed as ("wh"->"jonson") :
this is a word, jonsono contains "jonson"!
the word remove (remove "onson") as :
this is a word, jo contains "j"!
the 'w' in the text is in : 10 16 30 .
the "wh" in the text is in : 16 30 .
the word is:
this is a word, who contains "wh"!
the word has be changed as ("wh"->"jonson") :
this is a word, jonsono contains "jonson"!
the word remove (remove "onson") as :
this is a word, jo contains "j"!
分类:
C language
posted on 2008-10-27 20:53 volnet(可以叫我大V) 阅读(1861) 评论(7) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端