1

The issue i have with my code is as following: i cannot get my head around how to read each character and sum each up in one int for everyone at the end of all rotations. Here is my code:

class Program
{
    static void Main()
    {
        SortedDictionary<string, int> text = new SortedDictionary<string, int>();
        string[] characters = Console.ReadLine()
            .Split()
            .ToArray();

        foreach (var character in characters)
        {
            if (text.ContainsKey(character))
            {
                text[character]++;
            }
            else
            {
                text.Add(character, 1);
            }
        }
        foreach (var character in text)
        {
            Console.WriteLine($"{character.Key} -> {character.Value}");
        }
    }
}

I am reading here how many times a string exists in the Dictionary. What i need to get, written above, is different. Please help, thanks!

Sebastian 506563
  • 6,980
  • 3
  • 31
  • 56
Tom
  • 37
  • 8

2 Answers2

2

String.Split() is splitting on new lines by default so characters contains a single string with the whole line in it. If you want each of the characters, just get rid of the Split (and change the Dictionary KeyType to char to match the values):

SortedDictionary<char, int> text = new SortedDictionary<char, int>();
char[] characters = Console.ReadLine().ToArray();
// ...

https://www.ideone.com/hnMSv1

Since string implements IEnumerable<char> you actually don't even need to convert the characters into an array:

SortedDictionary<char, int> text = new SortedDictionary<char, int>();
string line = Console.ReadLine();
foreach( char character in line )
// ...

https://www.ideone.com/nLyBfC

clcto
  • 9,530
  • 20
  • 42
  • Thank you very much for the help so far! One question is in my head. Why doesnt it sort the values/keys in descending order anymore? I see it is still a SortedDictionary, but it doesnt sort them. – Tom Nov 06 '18 at 22:15
  • It is sorting them - it sorts by the keys not the values. `e < t < x` – clcto Nov 06 '18 at 22:17
  • how do i change the sorting to be done by values and not by keys then? – Tom Nov 06 '18 at 22:19
  • If you have other questions, they deserve their own questions. But the one you just asked is a duplicate, so see here: https://stackoverflow.com/questions/289/how-do-you-sort-a-dictionary-by-value – clcto Nov 06 '18 at 22:20
  • Thanks! Found the solution to my problem. Here it is: foreach (var character in text.OrderByDescending(x => x.Value)) { Console.WriteLine($"{character.Key} -> {character.Value}"); } – Tom Nov 06 '18 at 22:31
2

You can use LINQ here because any string consists of char element. So, string type implements IEnumerable<char> interface:

string str = "aaabbc";
var res = str
        .GroupBy(c => c)
        .ToDictionary(g => new { g.Key, Count = g.Count() });

The example below demonstrates how you can get it without casting to dictionary but projecting an anonymous type and sort the number of characters in descending order:

var res2 = str
    .GroupBy(c => c)
    .Select(d => new { d.Key, Count = d.Count() })
    .OrderByDescending(x => x.Count);
Dmitry Stepanov
  • 2,776
  • 8
  • 29
  • 45