今日学习任务
|
|
今日任务完成情况
|
|
今日开发中出现的问题汇总
|
|
今日未解决问题 | 对二维及二维以上数组的没能理解好 |
今日开发收获 |
|
自我评价
|
|
其他
| 无 |
1、输入一个字符串,同时输入帧头和帧尾(可以是多个字符),将该字符串中合法的帧识别出来。
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #define MAX_LEN sizeof(char) * 50
6
7 char *frame(char *ptr, char *head, char *tail)
8 {
9 char *fra_head;
10 char *fra_tail;
11 char *p_temp = ptr;
12
13 int n = strlen(head);
14 int m = strlen(tail);
15
16 while(*p_temp != 0)
17 {
18 if(strncmp(p_temp, head, n) == 0)
19 {
20 fra_head = p_temp;
21 p_temp = p_temp + n - 1;
22
23 while(*p_temp != 0)
24 {
25 if(strncmp(p_temp,tail,m) == 0)
26 {
27 fra_tail = p_temp;
28 p_temp = p_temp + m - 1;
29 }
30 p_temp++;
31 }
32 *(fra_tail + m) = '\0';
33 }
34 p_temp++;
35 }
36 return fra_head;
37 }
38
39 int main()
40 {
41 char *fra = (char *)malloc(MAX_LEN);
42 char *head = (char *)malloc(MAX_LEN);
43 char *tail = (char *)malloc(MAX_LEN);
44 char *ptr = (char *)malloc(MAX_LEN);
45
46 if(fra == NULL || head == NULL || tail == NULL || ptr == NULL)
47 {
48 printf("malloc error\n");
49 exit(1);
50 }
51
52 memset(fra,0,MAX_LEN);
53 memset(head,0,MAX_LEN);
54 memset(tail,0,MAX_LEN);
55 memset(ptr,0,MAX_LEN);
56
57 printf("Please input string:\n");
58 scanf("%s",ptr);
59 printf("Please input head and tail:\n");
60 scanf("%s",head);
61 scanf("%s",tail);
62
63 fra = frame(ptr, head, tail);
64
65 printf("result = %s\n",fra);
66
67 free(fra);
68 free(ptr);
69 free(head);
70 free(tail);
71
72 fra = NULL;
73 ptr = NULL;
74 head = NULL;
75 tail = NULL;
76
77 return 0;
78 }
运行结果:
Please input string:
qerfahellojfakljdfkalworlddkfjworldjdfjk
Please input head and tail:
hello world
result = hellojfakljdfkalworlddkfjworld