w3resource

C#: Convert the letters of a given string into alphabetical order


Alphabetical Order of String Letters

Write a C# Sharp program to convert the letters of a given string (same case-upper/lower) into alphabetical order.

Sample Solution:

C# Sharp Code:

using System;
using System.Linq;

namespace exercises
{
    class Program
    {
        // Main method where the program execution begins
        static void Main(string[] args)
        {
            // Display original strings and convert letters into alphabetical order
            Console.WriteLine("Original string: PHP");
            Console.WriteLine("Convert the letters of the said string into alphabetical order: " + test("PHP"));

            Console.WriteLine("Original string: javascript");
            Console.WriteLine("Convert the letters of the said string into alphabetical order: " + test("javascript"));

            Console.WriteLine("Original string: python");
            Console.WriteLine("Convert the letters of the said string into alphabetical order: " + test("python"));
        }

        // Function to convert the letters of a string into alphabetical order
        public static string test(string str1)
        {
            // Using LINQ's OrderBy method to sort characters in the string alphabetically
            // Converting the sorted characters back to a string using new string() and ToArray()
            return new string(str1.OrderBy(x => x).ToArray());
        }
    }
}

Sample Output:

Original string: PHP
Convert the letters of the said string into alphabetical order: HPP
Original string: javascript
Convert the letters of the said string into alphabetical order: aacijprstv
Original string: python
Convert the letters of the said string into alphabetical order: hnopty

Flowchart:

Flowchart: C# Sharp Exercises - Convert the letters of a given string into alphabetical order.

For more Practice: Solve these Related Problems:

  • Write a C# program to sort all words in a sentence alphabetically.
  • Write a C# program to sort all characters in a string in descending order.
  • Write a C# program to sort only vowels in a string, keeping consonants in place.
  • Write a C# program to rearrange string characters in alphabetical order, excluding digits.

Go to:


PREV : Check Array Average as Whole Number.
NEXT : Odd or Even Length of String.

C# Sharp Code Editor:



What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.