teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

<string.h> などの修正

2020/04/14 16:23

投稿

kazuma-s
kazuma-s

スコア8222

answer CHANGED
@@ -1,7 +1,7 @@
1
1
  こんなコードはいかがでしょうか?
2
2
  ```C
3
- #include <stdio.h> // scanf, puts
3
+ #include <stdio.h> // scanf, puts
4
- #include <strig.h> // strlen
4
+ #include <string.h> // strlen
5
5
 
6
6
  #define STR_SIZE 1000
7
7
  #define BUF_SIZE 20
@@ -10,13 +10,16 @@
10
10
  int main(void)
11
11
  {
12
12
  char str[STR_SIZE];
13
- int n, j = 0;
13
+ int n;
14
14
  if (scanf("%d", &n) != 1) return 1;
15
- for (int i = 0; i < n && j < STR_SIZE - BUF_SIZE - 1; i++) {
15
+ for (int i = 0, j = 0; i < n; i++) {
16
+ if (j >= STR_SIZE - BUF_SIZE) return 2;
16
17
  if (i > 0) str[j++] = '_';
17
18
  scanf(fmt, str + j);
18
19
  j += strlen(str + j);
19
20
  }
20
21
  puts(str);
21
22
  }
22
- ```
23
+ ```
24
+ **追記**
25
+ <string.h> など、ちょっと修正しました。