using
System;
namespace counter
{
class
Program
{
static
void Main(string[] args)
{
//
initialize an array of counters,
//
one for each letter in the ascii.
int[]
counter = new int[256];
for
(int i = 0; i < counter.Length; ++i)
counter = 0;
//
get input from the dumb user...
Console.Write("Enter a string: ");
string
s = Console.ReadLine();
//
do the calculations...
int
maxIndex = 1; // ☺
foreach
(char c in s)
if
(++counter[(int)c] > counter[maxIndex])
maxIndex = (int)c;
//
display output...
Console.WriteLine("most common char is `{0}` ({1} times).",
(char)maxIndex,
counter[maxIndex]);
}
}
} |