回答編集履歴

5

ソース修正

2018/12/06 11:05

投稿

cateye
cateye

スコア6851

test CHANGED
@@ -14,15 +14,25 @@
14
14
 
15
15
 
16
16
 
17
- 試してないけど・・・
17
+ 不思議の国のアリス序文から"Alice"を探す
18
18
 
19
19
  ```c
20
+
21
+ #include <stdio.h>
22
+
23
+ #include <string.h>
24
+
25
+
20
26
 
21
27
  #define YES (0 == 0)
22
28
 
23
29
  #define NO (!YES)
24
30
 
25
31
 
32
+
33
+ int cmp(const char *line, const char *str);
34
+
35
+ //
26
36
 
27
37
  int cmp(const char *line, const char *str)
28
38
 
@@ -38,9 +48,7 @@
38
48
 
39
49
  if (*line == ch) {
40
50
 
41
- line++;
42
-
43
- if (strncmp(line, str, len) == 0) {
51
+ if (strncmp(line+1, str, len) == 0) {
44
52
 
45
53
  return YES;
46
54
 
@@ -48,10 +56,84 @@
48
56
 
49
57
  }
50
58
 
59
+ line++;
60
+
51
61
  }
52
62
 
53
63
  return NO;
54
64
 
55
65
  }
56
66
 
67
+ //
68
+
69
+ int main( int agc, char *agv[])
70
+
71
+ {
72
+
73
+ if( agc != 3 ){
74
+
75
+ return 1;
76
+
77
+ }
78
+
79
+ //
80
+
81
+ FILE *fp= fopen(agv[1],"r");
82
+
83
+ if(fp == NULL ){
84
+
85
+ return 2;
86
+
87
+ }
88
+
89
+ char buf[256];
90
+
91
+ //
92
+
93
+ while(fgets(buf,sizeof buf,fp)){
94
+
95
+ if(cmp(buf,agv[2])){
96
+
97
+ puts(buf);
98
+
99
+ }
100
+
101
+ }
102
+
103
+ //
104
+
105
+ return 0;
106
+
107
+ }
108
+
109
+
110
+
57
111
  ```
112
+
113
+ usr~/test/c % ./a.out ../Alice.txt Alice
114
+
115
+ Alice was beginning to get very tired of sitting by her sister on the
116
+
117
+
118
+
119
+ it, ‘and what is the use of a book,’ thought Alice ‘without pictures or
120
+
121
+
122
+
123
+ There was nothing so VERY remarkable in that; nor did Alice think it so
124
+
125
+
126
+
127
+ Alice started to her feet, for it flashed across her mind that she had
128
+
129
+
130
+
131
+ In another moment down went Alice after it, never once considering how
132
+
133
+
134
+
135
+ dipped suddenly down, so suddenly that Alice had not a moment to think
136
+
137
+
138
+
139
+ 以下略

4

ソース修正

2018/12/06 11:05

投稿

cateye
cateye

スコア6851

test CHANGED
@@ -32,8 +32,6 @@
32
32
 
33
33
  size_t len = strlen(str);
34
34
 
35
- const char *lpt = &line[strlen(line - 1)];
36
-
37
35
 
38
36
 
39
37
  while (*line) {
@@ -48,14 +46,6 @@
48
46
 
49
47
  }
50
48
 
51
- line += len;
52
-
53
- if (line > lpt) {
54
-
55
- break;
56
-
57
- }
58
-
59
49
  }
60
50
 
61
51
  }

3

ソース修正

2018/12/06 10:33

投稿

cateye
cateye

スコア6851

test CHANGED
@@ -28,9 +28,11 @@
28
28
 
29
29
  {
30
30
 
31
- char ch = *str++;
31
+ char ch = *str++;
32
32
 
33
- size_t len = strlen(str);
33
+ size_t len = strlen(str);
34
+
35
+ const char *lpt = &line[strlen(line - 1)];
34
36
 
35
37
 
36
38
 
@@ -46,7 +48,13 @@
46
48
 
47
49
  }
48
50
 
49
- line += len; ←これはやばいかも(文字列の終端飛び越える:要確認)
51
+ line += len;
52
+
53
+ if (line > lpt) {
54
+
55
+ break;
56
+
57
+ }
50
58
 
51
59
  }
52
60
 

2

ソース追記

2018/12/06 10:25

投稿

cateye
cateye

スコア6851

test CHANGED
@@ -11,3 +11,49 @@
11
11
  5.違えば“なし”として関数を終了
12
12
 
13
13
  6.”有り”として関数を終了
14
+
15
+
16
+
17
+ 「試してないけど・・・」
18
+
19
+ ```c
20
+
21
+ #define YES (0 == 0)
22
+
23
+ #define NO (!YES)
24
+
25
+
26
+
27
+ int cmp(const char *line, const char *str)
28
+
29
+ {
30
+
31
+ char ch = *str++;
32
+
33
+ size_t len = strlen(str);
34
+
35
+
36
+
37
+ while (*line) {
38
+
39
+ if (*line == ch) {
40
+
41
+ line++;
42
+
43
+ if (strncmp(line, str, len) == 0) {
44
+
45
+ return YES;
46
+
47
+ }
48
+
49
+ line += len; ←これはやばいかも(文字列の終端飛び越える:要確認)
50
+
51
+ }
52
+
53
+ }
54
+
55
+ return NO;
56
+
57
+ }
58
+
59
+ ```

1

回答修正

2018/12/06 10:19

投稿

cateye
cateye

スコア6851

test CHANGED
@@ -1,17 +1,13 @@
1
- たぶん以下で行けるともいますが、
1
+ 考え方
2
2
 
3
- ```c
3
+ 1.検索文字列の先頭文字→ch
4
4
 
5
- while(fgets(s,sizeof s,fp)!=NULL){
5
+ 2.検索される文字列にchがあるか?
6
6
 
7
- if(strstr(s,argv[2])!=NULL){
7
+ 3.なければ“なし”として関数を終了
8
8
 
9
- puts(s);
9
+ 4.あれば、ch以降が検索文字列と同一か判断
10
10
 
11
- }
11
+ 5.違えば“なし”として関数を終了
12
12
 
13
- }
14
-
15
-
16
-
17
- ```
13
+ 6.”有り”として関数を終了