Created
November 7, 2018 09:22
-
-
Save Retterath/222186f0216e2b0f6c2271bfdb09ac58 to your computer and use it in GitHub Desktop.
Count Chars in a String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace CharInString | |
{ | |
class Program | |
{ | |
public static void Main() | |
{ | |
SortedDictionary<char, int> text = new SortedDictionary<char, int>(); | |
char[] characters = Console.ReadLine() | |
.Where(x => !Char.IsWhiteSpace(x)) | |
.ToArray(); | |
foreach (var character in characters) | |
{ | |
if (text.ContainsKey(character)) | |
{ | |
text[character]++; | |
} | |
else | |
{ | |
text.Add(character, 1); | |
} | |
} | |
foreach (var character in text.OrderByDescending(x => x.Value)) | |
{ | |
Console.WriteLine($"{character.Key} -> {character.Value}"); | |
} | |
} | |
//var text = new SortedDictionary<char, int>(); | |
//var inputText = Console.ReadLine(); | |
// foreach (var character in inputText) | |
// { | |
// if (character == ' ') | |
// { | |
// continue; | |
// } | |
// if (text.ContainsKey(character)) | |
// { | |
// text[character]++; | |
// } | |
// else | |
// { | |
// text.Add(character, 1); | |
// } | |
// } | |
// foreach (var character in text.OrderByDescending(x => x.Value)) | |
// { | |
// Console.WriteLine($"{character.Key} -> {character.Value}"); | |
// } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment