回答編集履歴

2

列位置指定版追記

2016/06/30 09:58

投稿

catsforepaw
catsforepaw

スコア5938

test CHANGED
@@ -41,3 +41,55 @@
41
41
  ```
42
42
 
43
43
  `strtok`関数は文字列を区切り文字で切り出したいときによく使います。
44
+
45
+
46
+
47
+ ---
48
+
49
+ 列位置指定番です。
50
+
51
+ columnには一番左を1とした列位置を渡します。
52
+
53
+ ```C
54
+
55
+ int match_str_column(const char *a, const char *b, int column)
56
+
57
+ {
58
+
59
+ char buf[1000];
60
+
61
+ char *str;
62
+
63
+ char *tok;
64
+
65
+ int count = 0; // 列位置カウント用
66
+
67
+ strcpy(buf, a);
68
+
69
+ tok = strtok(buf, " \t\r\n");
70
+
71
+ if(tok == NULL)
72
+
73
+ return 0;
74
+
75
+ while(tok != NULL)
76
+
77
+ {
78
+
79
+ str = tok;
80
+
81
+ if(++count == column) // 指定した列位置を取り出したら
82
+
83
+ break; // ループを抜ける
84
+
85
+ tok = strtok(NULL, " \t\r\n");
86
+
87
+ }
88
+
89
+ return (strcmp(str, b) == 0) ? 1 : 0;
90
+
91
+ }
92
+
93
+ ```
94
+
95
+

1

行内に有効な文字列がない場合のチェックを追加

2016/06/30 09:58

投稿

catsforepaw
catsforepaw

スコア5938

test CHANGED
@@ -20,6 +20,10 @@
20
20
 
21
21
  tok = strtok(buf, " \t\r\n");
22
22
 
23
+ if(tok == NULL)
24
+
25
+ return 0;
26
+
23
27
  while(tok != NULL)
24
28
 
25
29
  {