static void Main(string[] args)
{
Dictionary<char, int> counter = new Dictionary<char, int>();
string s = "alsdk 0poqwer rkj kerie qwel; wejw ";
s = s.ToLower(); // 全小写
char max = s[0];
foreach (char c in s)
{
if (!char.IsLetter(c)) continue; // 非字母
if (counter.ContainsKey(c))
{
counter[c]++;
}
else
{
counter.Add(c, 1);
}
if (counter[max] < counter[c]) max = c;
}
Console.WriteLine("出现最多的字符是:'{0}' 共出现:{1}次", max, counter[max]);
Console.ReadLine();
}
本文介绍了一个使用 C# 编写的简单字符统计程序。该程序通过 Dictionary 结构记录输入字符串中每个字母出现的次数,并找出出现次数最多的字母。

1342

被折叠的 条评论
为什么被折叠?



