文字列の配列の各要素に指定した文字が何個含まれるか
表示するプログラムを作りたいのですが、
実行例
./a.out
Input a character: o
nagoya: 1
kobe: 1
narakoen: 1
otsu: 1
kyoto: 2
としたいのですが、私のコードだとエラーが起こります。
どこに問題があるのか、どのように直せばいいのか分かりません。
私のプログラムです。
C
1#include<stdio.h> 2#define NUM 5 3#define LEN 100 4 5int main(){ 6 char stations[NUM][LEN] = {"nagoya", "kobe", "narakoen","otsu", "kyoto"}; 7 int i; 8 int num = 0; 9 char chr; 10 printf("Input a character:"); 11 scanf("%s",&chr); 12 num = search(stations, chr); 13 for(i=0;i<NUM;i++){ 14 printf("%s: %d\n",stations[i][LEN],num); 15 } 16 return 0; 17} 18int search(char *str, char *chr) 19{ 20 int cnt = 0; 21 while( *str != '\0' ) { 22 if( *str == *chr ) { 23 cnt++; 24 } 25 str++; 26 } 27 return(cnt); 28} 29 30 31 32
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/21 11:14
2017/01/21 12:26
2017/01/21 12:45