maajanes
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace CountCharacters
{
static class Program
{
static void Main()
{
Dictionary<Char, int> countChars = new Dictionary<Char,int>();
String test = "Hello man how's it going. Some characters here in this string. Some stuff to test our character counter with.";
foreach (Char ch in test)
{
if(countChars.ContainsKey(ch))
{
countChars[ch]++;
}
else
{
countChars.Add(ch, 1);
}
}
List<CharInt> list = new List<CharInt>();
foreach (Char key in countChars.Keys)
{
list.Add(new CharInt(key, countChars[key]));
}
list.Sort();
foreach (CharInt ci in list)
{
Console.WriteLine("{0} : {1}", ci.Char, ci.Int);
}
Console.ReadLine();
}
private class CharInt : IComparable<CharInt>
{
public Char Char;
public int Int;
public CharInt(Char c, int i)
{
this.Char = c;
this.Int = i;
}
public int CompareTo(CharInt other)
{
if (other == null) return 1;
return -1 * this.Int.CompareTo(other.Int);
}
}
}
}