------------------ Windows Phones 7手机开发、.Net培训、期待与您交流! ----------------------
1 class Program
2 {
3
4
5 //10、 计算字符串中每种字符出现的次数(面试题)。“Welcome to Chinaworld”,不区分大小写,打印“W 2”“e 2”“l 3”……
6 static void Main(string[] args)
7 {
8 string str = "Welcome to Chinaworld";
9 str = str.ToLower();
10 Dictionary<char, int> dic = new Dictionary<char, int>();
11 for (int i = 0; i < str.Length; i++)
12 {
13 //判断是否是字符
14 if (char.IsLetter(str[i]))
15 {
16 //判断字典中是否有此字符
17 if (!dic.ContainsKey(str[i]))
18 {
19 dic.Add(str[i], 1);
20 }
21 else
22 {
23 dic[str[i]]++;
24 }
25 }
26 }
27 foreach (KeyValuePair<char, int> item in dic)
28 {
29 Console.WriteLine("{0}字符出现的次数是{1}次", item.Key, item.Value);
30 }
31 Console.ReadKey();
32 }
33 }
------------------ Windows Phones 7手机开发、.Net培训、期待与您交流! ----------------------