C#: Sort characters in a string
Sort Characters in String
Write a C# Sharp program to sort characters in a given string (uppercase/lowercase letters and numbers). Return the newly sorted string.
Sample Data:
("AAAbfed231") -> "AAAbdef123"
(" ") -> "Blank string!"
("Python") -> "hnoPty"
("W3resource") -> "ceeorrsuW3"
Sample Solution:
C# Sharp Code:
using System;
using System.Linq;
namespace exercises
{
class Program
{
static void Main(string[] args)
{
// Initialize and process different input strings
string text = "AAAbfed231";
Console.WriteLine("Original string: " + text);
Console.WriteLine("Sorted string: " + test(text));
text = " ";
Console.WriteLine("Original string: " + text);
Console.WriteLine("Sorted string: " + test(text));
text = "Python";
Console.WriteLine("Original string: " + text);
Console.WriteLine("Sorted string: " + test(text));
text = "W3resource";
Console.WriteLine("Original string: " + text);
Console.WriteLine("Sorted string: " + test(text));
}
// Method to sort and concatenate characters and digits in a string
public static string test(string text)
{
// Check if the input string is null, empty, or consists only of whitespace characters
bool flag = string.IsNullOrWhiteSpace(text);
// If the string is blank, return "Blank string!"
if (flag)
return "Blank string!";
// Extract digits and sort them in ascending order
var text_nums = text.Where(char.IsDigit).OrderBy(el => el).ToList();
// Extract letters, convert them to lowercase, sort them alphabetically, then by descending original character order
var text_chars = text.Where(char.IsLetter)
.Select(el => new { l_char = char.ToLower(el), _char = el })
.OrderBy(el => el.l_char)
.ThenByDescending(el => el._char)
.ToList();
// Concatenate the sorted characters and digits and return the resulting string
return new string(text_chars.Select(el => el._char).Concat(text_nums).ToArray());
}
}
}
Sample Output:
Original string: AAAbfed231 Sorted string: AAAbdef123 Original string: Sorted string: Blank string! Original string: Python Sorted string: hnoPty Original string: W3resource Sorted string: ceeorrsuW3
For more Practice: Solve these Related Problems:
- Write a C# program to sort characters in a string in case-insensitive order followed by numeric sort.
- Write a C# program to sort characters in reverse order but keep the digits in ascending order.
- Write a C# program to group digits, uppercase, and lowercase letters, then sort within each group.
- Write a C# program to sort only alphabetic characters in a string and keep other characters in place.
Go to:
PREV : Create Identity Matrix.
NEXT : Compare Equality of Three Integers.
C# Sharp Code Editor:
What is the difficulty level of this exercise?