Answer to Question #347705 in C# for Soni

Question #347705

Write a program that asks the users to enter five characters. Then the program should display the following menu



a. Sort in Ascending order



b. Sort in Descending order



The program should perform sorting of these five characters in ascending or descending order as per the user requirement




1
Expert's answer
2022-06-03T07:56:04-0400
string? chars = "";

Console.WriteLine("To exit the program, enter 'exit'.");
Console.WriteLine("");

while (chars == null || chars.Length != 5) {
    Console.Write("Enter five characters: ");
    chars = Console.ReadLine();
    if (chars == "exit") return;
}

SelectTypeSorted(chars);

Console.WriteLine("To end the program, press Enter.");
Console.ReadLine();


//Auxiliary methods
static void SelectTypeSorted(string? chars)
{
    char[] bufferChars = chars.ToCharArray();
    Console.WriteLine("");
    Console.WriteLine("Choose the sorting method: ");
    Console.WriteLine("a. Sort in Ascending order");
    Console.WriteLine("b. Sort in Descending order");
    
    switch (Console.ReadLine())
    { 
        case "a": Array.Sort(bufferChars);
                  Console.WriteLine("");
                  Console.WriteLine(bufferChars);
                  Console.WriteLine("");
                  break;

        case "b": Array.Sort(bufferChars);
                  Console.WriteLine("");
                  Console.WriteLine(bufferChars.Reverse().ToArray());
                  Console.WriteLine("");
                  break;

        case "exit": return;


        default: SelectTypeSorted(chars);
                 break;

    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS