文字列のなかで指定された文字を数えるプログラムを作りたいのですが、どのように書けばいいのかわからなくなりました。
実行例
$ ./a.out
文字列を入力してください。
hogehigehagehoge
探したい文字列を入力してください。
oge
「hogehigehagehoge」の中に「oge」のいずれかの文字は10個あります。
この”いずれかの文字”を数える方法が分かりません。
私のコードです。
C
1#include <stdio.h> 2int search_a(char *str); 3int main(void) 4{ 5 char str[100]; 6 char ter[100]; 7 int num_ter = 0; 8 printf("文字列を入力してください。\n"); 9 scanf("%99s", str); 10 printf("探したい文字列を入力してください。"); 11 scanf("%99s",ter); 12 num_ter = search_ter(str); 13 printf("「%s」 の中に「%s」のいずれかの文字は%d個あります。\n",str,ter, num_ter); 14 return(0); 15} 16int search_ter(char *str) 17{ 18 int cnt = 0; 19 while(*str != '\0') { 20 if( str == ter ) { 21 cnt++; 22 } 23 str++; 24 } 25 return cnt; 26} 27```21行目の記述がおかしいことは分かるのですが、、 28アドバイスいただければ幸です。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/15 07:52