class Program
{
public static Dictionary<char, int> Count(string stringToCount)
{
Dictionary<char, int> charcount = new Dictionary<char, int>();
foreach (var charchar in stringToCount)
{
if (!charcount.ContainsKey(charchar))
{
charcount.Add(charchar, 1);
}
else
{
charcount[charchar]++;
}
}
return charcount;
}
static void Main(string[] args)
{
Console.WriteLine("Enter the string :");
string str = Console.ReadLine();
var count = Program.Count(str);
foreach (var character in count)
{
Console.WriteLine("{0}-{1}", character.Key, character.Value);
}
Console.WriteLine("Total number of characters: {0}", str.Count());
Console.ReadLine();
}
}
Output:
No comments:
Post a Comment