您的位置:首页 > 编程语言 > C#

C# 在一个字符串中,找出出现次数最多的字母,并计算次数?

2012-07-18 16:36 239 查看
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();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐