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
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;
}
}
Comments
Leave a comment