题目描述
编写一个实例方法getCountChar方法。该方法参数有两个,第一个参数可以是字符串s,第二个参数为字符c,方法返回值为第二个参数在第一个参数中出现次数。例如,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){
//
在此处填写代码
//
}
}
}
using System;
using System.IO;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
namespace myApp
{
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;
char[] a = s.ToCharArray();
for (int i = 0; i < a.Length; i++)
{
if (a[i].Equals(c))
{
count++;
}
}
return count;
}
}
}