Count of each charecter in a string
//How to count the occurrence of each character in a string
string sentence = "hello world";
Dictionary<char, int> ditCounts = new Dictionary<char, int>();
for (int i = 0; i < sentence.Length; i++)
{
if (sentence[i] != ' ')
{
if (!ditCounts.ContainsKey(sentence[i]))
{
ditCounts.Add(sentence[i], 1);
}
else
{
ditCounts[sentence[i]]++;
}
}
}
foreach (var item in ditCounts)
{
Console.WriteLine(item.Key + " - " + item.Value);
}
Console.ReadKey();