/
21. LetterCounter.cs
43 lines (35 loc) · 1.24 KB
/
21. LetterCounter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Write a program that reads a string from the console and prints all different letters in the string along with
// information how many times each letter is found.
using System;
using System.Collections.Generic;
using System.Linq;
public class LetterCounter
{
public static void Main()
{
Console.WriteLine("Please enter a sentence or two. Then press Enter for the letter count:");
string[] specialSigns = { " ", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+" };
string input = Console.ReadLine();
for (int i = 0; i < specialSigns.Length; i++)
{
input = input.Replace(specialSigns[i], string.Empty);
}
Dictionary<char, int> allLetters = new Dictionary<char, int>();
for (int i = 0; i < input.Length; i++)
{
if (allLetters.ContainsKey(input[i]))
{
allLetters[input[i]]++;
}
else
{
allLetters.Add(input[i], 1);
}
}
var sortedLetters = allLetters.OrderBy(x => x.Key);
foreach (var item in sortedLetters)
{
Console.WriteLine("Letter --> {0} - Number --> {1}", item.Key, item.Value);
}
}
}