文字列"UniversalStudiosJapan"を対象にし、指定した文字がいくつ含まれるかを表示するプログラム。
①main関数での処理
1.キーボードから探したい文字を取得する。
2.count関数を呼び出す("UniversalStudiosJapan"と検索文字を渡す)
3.何個含まれるかを表示する。
②count関数での処理
1.検索対象文字列と検索文字を受け取る
2.文字列中に検索文字が何個含まれるかを数えて結果を返す※int型
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(); printf("文字列 UniversalStudiosJapan の中に含まれている%sの数は%d¥n",ch,n); return 0; } コード
回答1件
あなたの回答
tips
プレビュー