タイトル説明
インスタンスメソッドgetCountCharメソッドを記述します。このメソッドには2つのパラメーターがあります。最初のパラメーターは文字列s、2番目のパラメーターは文字c、メソッドの戻り値は2番目のパラメーターが最初のパラメーターに出現する回数です。たとえば、CountChar( "6221982"、 '2')は値3を返します。
プログラムコードの一部が提供されています。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{ class Program { static void Main(string [] args) { string str = Console.ReadLine( ); char ch =(char)Console.Read(); Program pro = new Program(); Console.WriteLine(pro.getCountChar(str、ch)); //Console.ReadKey(); }
public int getCountChar(String s、char c){ // ここにコードを入力します // } } }
送信する際は、質問全体を送信してください。!!
入る
文字列s、文字c
出力
文字列s内の文字cの出現回数
サンプル入力
6221982 2
サンプル出力
3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = Console.ReadLine();
char ch = (char)Console.Read();
Program pro = new Program();
Console.WriteLine(pro.getCountChar(str, ch));
//Console.ReadKey();
}
public int getCountChar(String s,char c){
int count = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == c)
{
count++;
}
}
return count;
}
}
}