---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------
计算一段字符串中每个字符出现的次数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _04面试题
{
class Program
{
static void Main(string[] args)
{
//统计一个字符串中每种字母出现的字数
//welcome to china
Dictionary<char, int> dic = new Dictionary<char, int>();
string str = "welcome to china";
for (int i = 0; i < str.Length; i++)
{
if (str[i] == ' ')
{
continue;
}
//表示前面已经出现过了
if (dic.ContainsKey(str[i]))
{
//让这个小写字母所对应的出现次数 +1
dic[str[i]]++;
}
else
{
dic[str[i]] = 1;
}
}
foreach (KeyValuePair<char,int> kv in dic)
{
Console.WriteLine("小写字母{0}出现了{1}次",kv.Key,kv.Value);
}
Console.ReadKey();
}
}
}
---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------