Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

<< Back to C-SHARP

C# Alphabetize String

Implement a string alphabetize method to sort the letters in strings.
Alphabetize string. An individual string can be alphabetized. This rearranges the letters so they are sorted A to Z. These strings with sorted letters can be used as lookup keys. Here we see a method that alphabetizes strings in the C# programming language.SortStrings
To start, we see the Alphabetize method, which receives a C# string and returns a sorted copy of that string. Please recall that strings are immutable. This means we cannot sort the letters in-place.

Method: The method converts the string to a char array using ToCharArray, which you can modify in-place. It uses the static Array.Sort method.

And: The final line in the method creates a new string from the char array it modified, and returns it.

Char ArrayArray.Sort

Char array: With char arrays, you can change a character in a char array all by itself, which you cannot do with a string. This is much faster.

C# program that alphabetizes strings using System; class Program { static void Main() { Console.WriteLine(StringTools.Alphabetize("sam")); Console.WriteLine(StringTools.Alphabetize("spot")); Console.WriteLine(StringTools.Alphabetize("cat")); } } public static class StringTools { /// <summary> /// Alphabetize the characters in the string. /// </summary> public static string Alphabetize(string s) { // 1. // Convert to char array. char[] a = s.ToCharArray(); // 2. // Sort letters. Array.Sort(a); // 3. // Return modified string. return new string(a); } } Output ams opst act
Already alphabetical. Some strings are already in alphabetical order. We can avoid sorting these strings. This enhances performance and results in simpler code. This method determines if the characters in a string are alphabetized.

Caution: This method compares the numeric values of chars, so it won't work right on uppercase letters.


IsAlpha: This method contains a foreach iterative statement that loops through each character in the string.

And: If it finds a character that is lower in its ordinal value than the previous character, it returns false.

Also: It returns early when it finds an incorrect (non-alphabetized) character. In this situation, no further processing is needed.

C# program that checks alphabetical strings public static class StringTools { /// <summary> /// Alphabetize string, checking to see if it is already alphabetized. /// </summary> public static string AlphabetizeCheck(string s) { if (IsAlpha(s)) { return s; } else { // TODO: Sort and return the new string. return ""; } } /// <summary> /// See if letters in string are in numeric ascending order. /// </summary> public static bool IsAlpha(string s) { char prev = char.MinValue; foreach (char c in s) { if (c < prev) { return false; } prev = c; } return true; } }
Summary. We saw approaches for alphabetizing strings. It is typically a bad idea to use nested for-loops—that is often a slower solution. This code may not work on mixed-case strings. You can remedy this by using ToLower in Alphabetize.ToLower
The Dev Codes