#5
by かずま » 8年前
mac さんが書きました:解決しました
次のような実行結果で、本当に解決ですか?
コード:
探索文字cを入力
3
c:3
0
0
1
1
1
1
文字列 s は、数字だけの列なんですか?
私なら次のようにします。
コード:
#include <stdio.h>
void chum(const char *s, int c)
{
int count = 0;
for (int i = 0; s[i]; i++) {
if (s[i] == c) {
printf(" %d", i);
count++;
}
}
printf(": count = %d\n", count);
}
int main(void)
{
char c, s[] = "123456abc4321";
puts("探索文字cを入力");
scanf(" %c", &c);
printf("s: %s\n", s);
printf("c: %c\n", c);
chum(s, c);
return 0;
}
実行結果
コード:
探索文字cを入力
3
s: 123456abc4321
c: 3
2 10: count = 2
c は、文字列 s の中の 2番目と10番目にあり、全部で 2個ありました。
実行結果その2
コード:
探索文字cを入力
b
s: 123456abc4321
c: b
7: count = 1
c は、文字列 s の中の 7番目にあり、全部で 1個ありました。
[quote="mac" id=3,19382,146526]解決しました[/quote]
次のような実行結果で、本当に解決ですか?
[code=text]
探索文字cを入力
3
c:3
0
0
1
1
1
1
[/code]
文字列 s は、数字だけの列なんですか?
私なら次のようにします。
[code=c]
#include <stdio.h>
void chum(const char *s, int c)
{
int count = 0;
for (int i = 0; s[i]; i++) {
if (s[i] == c) {
printf(" %d", i);
count++;
}
}
printf(": count = %d\n", count);
}
int main(void)
{
char c, s[] = "123456abc4321";
puts("探索文字cを入力");
scanf(" %c", &c);
printf("s: %s\n", s);
printf("c: %c\n", c);
chum(s, c);
return 0;
}
[/code]
実行結果
[code=text]
探索文字cを入力
3
s: 123456abc4321
c: 3
2 10: count = 2
[/code]
c は、文字列 s の中の 2番目と10番目にあり、全部で 2個ありました。
実行結果その2
[code=text]
探索文字cを入力
b
s: 123456abc4321
c: b
7: count = 1
[/code]
c は、文字列 s の中の 7番目にあり、全部で 1個ありました。