I've written some code that uses a SortedDictionary
to count the number of occurrences of a character in a string.
How can I improve this code? What should I be doing differently? Any advice at all would be helpful.
static void Main(string[] args)
{
string longText = @"The quick brown fox jumps over the lazy dog";
var count = CharacterCount.Count(longText);
foreach (var character in count)
{
Console.WriteLine("{0} - {1}", character.Key, character.Value);
}
}
CharacterCount class:
class CharacterCount
{
public static SortedDictionary<char, ulong> Count(string stringToCount)
{
SortedDictionary<char, ulong> characterCount = new SortedDictionary<char, ulong>();
foreach (var character in stringToCount)
{
if (!characterCount.ContainsKey(character))
{
characterCount.Add(character, 1);
}
else
{
characterCount[character]++;
}
}
return characterCount;
}
Here is the output: