質問編集履歴
1
訂正後のコードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -79,3 +79,77 @@
|
|
79
79
|
なぜ、if (s[i] == c) return i;としては、いけないのか理由が知りたいです。
|
80
80
|
|
81
81
|
よろしくお願いします。
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
下記は訂正後のコードです
|
86
|
+
|
87
|
+
```
|
88
|
+
|
89
|
+
#define _CRT_SECURE_NO_WARNINGS
|
90
|
+
|
91
|
+
#include <stdio.h>
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
int str_char(const char s[], int c)
|
96
|
+
|
97
|
+
{
|
98
|
+
|
99
|
+
int i;
|
100
|
+
|
101
|
+
int len = 0;
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
while (s[len])
|
106
|
+
|
107
|
+
len++;
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
for (i = 0; i < len; i++) {
|
112
|
+
|
113
|
+
if (s[i] == c)
|
114
|
+
|
115
|
+
return i;
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
return -1;
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
int main(void)
|
126
|
+
|
127
|
+
{
|
128
|
+
|
129
|
+
char str[128];
|
130
|
+
|
131
|
+
int a;
|
132
|
+
|
133
|
+
char co;
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
printf("文字列を入力してください\n");
|
138
|
+
|
139
|
+
scanf("%s", str);
|
140
|
+
|
141
|
+
printf("文字を入力してください\n");
|
142
|
+
|
143
|
+
scanf(" %c", &co);
|
144
|
+
|
145
|
+
a = str_char(str, co);
|
146
|
+
|
147
|
+
printf("文字列の中にある入力した文字の添え字は%d\n", a);
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
return 0;
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
```
|