Sort String In Alphabetical Order In C#

Here I’m going to write program logic to sort string in alphabetical order in C#.Net.
 
It is one of the most important interview questions for fresher as well as experienced candidates. It is a very important program where you will get the result in just 3-4 lines of code using two for loops.
 
Check this video for your reference. 
  1. using System;  
  2. namespace ConsoleAppInterviewLogical  
  3. {  
  4.   class SortStringAlpha  
  5.   {  
  6.     static void Main(string[] args)  
  7.     {  
  8.         char temp;  
  9.         string myStr = "Gautam"// Here you can take input from user and assign to myStr variable  
  10.         string str = myStr.ToLower();  
  11.         char[] charstr = str.ToCharArray();  
  12.         for(int i=1;i< charstr.Length;i++)  
  13.         {  
  14.             for(int j=0;j< charstr.Length-1;j++)  
  15.             {  
  16.                 if(charstr[j]> charstr[j+1])  
  17.                 {  
  18.                     temp = charstr[j];  
  19.                     charstr[j] = charstr[j + 1];  
  20.                     charstr[j + 1] = temp;  
  21.                 }  
  22.            }  
  23.        }  
  24.        Console.WriteLine(charstr); //aagmtu  
  25.        Console.ReadLine();  
  26.      }  
  27.    }  
  28. }  
Hope you like the post, if you found this post useful, please like the post and provide your feedback. that motivates me to create more useful stuff. 
Ebook Download
View all
Learn
View all