回答編集履歴
3
コードの追加
answer
CHANGED
@@ -6,6 +6,7 @@
|
|
6
6
|
|
7
7
|
```C
|
8
8
|
#include <stdio.h>
|
9
|
+
#include <string.h>
|
9
10
|
#include <stdlib.h>
|
10
11
|
|
11
12
|
#define BUFSIZE 256
|
@@ -13,6 +14,27 @@
|
|
13
14
|
int main(void)
|
14
15
|
{
|
15
16
|
char buf[BUFSIZE];
|
17
|
+
printf("文字列を入力してEnterボタンを押してください\n");
|
18
|
+
|
19
|
+
fgets(buf,sizeof(buf),stdin);
|
20
|
+
|
21
|
+
printf("入力した文字列の長さは%dです。",strlen(buf) - 1 );
|
22
|
+
|
23
|
+
return 0;
|
24
|
+
}
|
25
|
+
```
|
26
|
+
<追記>
|
27
|
+
原始的な方法でやってみました。
|
28
|
+
文字列の語尾に行くまでfor文を用いて長さを求めています。
|
29
|
+
```C
|
30
|
+
#include <stdio.h>
|
31
|
+
#include <stdlib.h>
|
32
|
+
|
33
|
+
#define BUFSIZE 256
|
34
|
+
|
35
|
+
int main(void)
|
36
|
+
{
|
37
|
+
char buf[BUFSIZE];
|
16
38
|
int i;
|
17
39
|
int length = 0;
|
18
40
|
printf("文字列を入力してEnterボタンを押してください\n");
|
@@ -27,4 +49,5 @@
|
|
27
49
|
|
28
50
|
return 0;
|
29
51
|
}
|
52
|
+
|
30
53
|
```
|
2
ソースの変更
answer
CHANGED
@@ -6,18 +6,24 @@
|
|
6
6
|
|
7
7
|
```C
|
8
8
|
#include <stdio.h>
|
9
|
-
#include <
|
9
|
+
#include <stdlib.h>
|
10
10
|
|
11
11
|
#define BUFSIZE 256
|
12
12
|
|
13
13
|
int main(void)
|
14
14
|
{
|
15
15
|
char buf[BUFSIZE];
|
16
|
+
int i;
|
16
|
-
|
17
|
+
int length = 0;
|
17
18
|
printf("文字列を入力してEnterボタンを押してください\n");
|
19
|
+
|
18
20
|
fgets(buf,sizeof(buf),stdin);
|
19
21
|
|
22
|
+
|
23
|
+
for(i = 0; i < buf[i + 1] != '\0'; i++){
|
24
|
+
length++;
|
25
|
+
}
|
20
|
-
printf("入力した文字列の長さは%d",
|
26
|
+
printf("入力した文字列の長さは%dです。",length);
|
21
27
|
|
22
28
|
return 0;
|
23
29
|
}
|
1
ソースの変更
answer
CHANGED
@@ -8,9 +8,12 @@
|
|
8
8
|
#include <stdio.h>
|
9
9
|
#include <string.h>
|
10
10
|
|
11
|
+
#define BUFSIZE 256
|
12
|
+
|
11
13
|
int main(void)
|
12
14
|
{
|
13
|
-
char
|
15
|
+
char buf[BUFSIZE];
|
16
|
+
double f;
|
14
17
|
printf("文字列を入力してEnterボタンを押してください\n");
|
15
18
|
fgets(buf,sizeof(buf),stdin);
|
16
19
|
|