问题描述
我有一个我在C#中实现的程序,它应该按字母顺序输出每个字母的次数 发生在文本中?
I have a program I am implementing in C# that should output the number of times each letter in alphabetical order occurs in text?
有人能指出我正确的方向吗
Can someone point me in the right direction
谢谢,
Ion
推荐答案
Dictionary< char,int> countOfLetters = new Dictionary< char,int>();
Dictionary<char, int> countOfLetters = new Dictionary<char, int>();
现在你可以使用foreach循环扫描字符串
Now you can scan the string using a foreach loop
foreach(string coScan中的char c) )
foreach(char c in stringToScan)
在循环内你可以检查字符(字母)是否已经添加
Inside the loop you can check if the character(letter) is already added or not
if(countOfLetters.ContainsKey(c))然后你应该增加点数
if(countOfLetters.ContainsKey(c)) then you should increment the count
countOfLetters [c] ++;
countOfLetters[c]++;
否则你需要将它添加到字数为1的
else you need to add it to the dictionary with count 1
countOfLetters.Add(c,1);
countOfLetters.Add(c, 1);
现在你可以使用另一个foreach循环打印结果
Now you can print the result using another foreach loop
foreach( charc in countOfLetters.Keys)
foreach(char c in countOfLetters.Keys)
Console.WriterLine(" Letter" + c +"出现" + countOfLetter [c] .ToString()+" times。" ;);
Console.WriterLine("Letter " + c + " appeared " + countOfLetter[c].ToString() + " times.");
希望这会有所帮助!!!
Hope this helps!!!
这篇关于计算每个字母按字母顺序出现在文本中的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!