I am sorting a input from a string with maximum 100 characters. Where do I need to set the reverse string by the position of characters?

This is for a C# programming in Visual Studio, but I have tried to compile it and the end of program cannot be result.

namespace Testing_2
    class Program
        static void Main(string[] args)
            string Kata;
            Console.WriteLine("Masukan Kata :");
            Kata = Console.ReadLine();
            Kata = new string(Kata.Reverse().ToArray());



I expect the output of HELLO WORLD to be HELLOOWRD but the actual output is Masukan Kata : HELLO WORLD


Press any key to continue . . .

2 Answers

Sweeper On

According to your comments, you seem to want to group the same characters together.

You can call GroupBy here. Reverse is irrelevant.

string input = "HELLO WORLD";
var groups = input.GroupBy(x => x);
foreach(var group in groups) {
    foreach (var element in group) {

// Output: HELLLOO WRD

You can remove the spaces before grouping if you want, using Replace:

input = input.Replace(" ", "");
ryans610 On

I have something just like @Sweeper's answer, only using Linq for better readability´╝Ü

 var input = "HELLO WORLD";
 var result = string.Join(string.Empty, input.GroupBy(x => x).SelectMany(x => x));
 Console.Write(result);    // HELLLOO WRD

SelectMany() is a linq extension method for flattens enumerable.