当前位置:编程学习 > C#/ASP.NET >>

C#统计字符出现个数

做毕设时,需要统计汉语语料库中单词(单词已经粗切分)出现的频率,想到与我们开始学习C语言时统计字符串中每个字符出现的个数类似,就想用C#实现一下,发现泛型集合类Dictionary很有用,几行代码就搞定,爽!

 1         private static void FnCountWord()
2         {
3             String text = Console.ReadLine();
4             Dictionary<Char, int> charDict = new Dictionary<char, int>();
5             for (int i = 0; i < text.Length; i++)
6             {
7                 if (charDict.ContainsKey(text[i]) == true)
8                 {
9                     charDict[text[i]]++;
10                 }
11                 else
12                 {
13                     charDict.Add(text[i], 1);
14                 }
15             }
16             //int编号,KeyValuePair<Char, int>中char字符,int统计字符数量
17              Dictionary<int, KeyValuePair<Char, int>> charID = new Dictionary<int, KeyValuePair<Char, int>>();
18             int k = 0;
19             foreach (KeyValuePair<Char, int> charInfo in charDict)
20 &

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,