文字列"UniversalStudiosJapan"を対象にし、指定した文字がいくつ含まれるかを表示するプログラム。
①main関数での処理
1.キーボードから探したい文字を取得する。
2.count関数を呼び出す("UniversalStudiosJapan"と検索文字を渡す)
3.何個含まれるかを表示する。
②count関数での処理
1.検索対象文字列と検索文字を受け取る
2.文字列中に検索文字が何個含まれるかを数えて結果を返す※int型
なぜcntが機能しないのか、
countChar関数に"Universal Studios Japan",検索文字を渡す)
とはどういうことか、 引数は何を使えばいいのか困っています
int countChar(char str[],int c) { int cnt=0; int i; for(i=0;str[i]!='¥0';i++){ if(str[i]==c){ cnt++; return cnt; } } } int main(void) { int n; char str [256]="UniversalStudiosJapan"; char ch[10]; printf(">"); scanf("%s",ch); n=countChar(str,ch); printf("文字列 UniversalStudiosJapan の中に含まれている%sの数は%d¥n",ch,n); return 0; } コード
全角が入っています。→ '¥0' と¥n ←環境の問題??
> if(str[i]==c){
cnt++;
return cnt;
見つかったら即戻っていますが・・・
回答2件
あなたの回答
tips
プレビュー