Blog

Sort a string alphabetically

Coding Problems (C#)

Sort a string alphabetically

				
					public static void SortAlphabatically()
        {
            char temp;
            string myStr = "Gautam"; // Here you can take input from user and assign to myStr variable  
            string str = myStr.ToLower();
            char[] charstr = str.ToCharArray();
            for (int i = 1; i < charstr.Length; i++)
            {
                for (int j = 0; j < charstr.Length - 1; j++)
                {
                    if (charstr[j] > charstr[j + 1])
                    {
                        temp = charstr[j];
                        
                        charstr[j] = charstr[j + 1];
                        var a = charstr[j];
                        charstr[j + 1] = temp;
                        var b = charstr[j + 1];
                    }
                }
            }
            Console.WriteLine(charstr); //aagmtu  
            Console.ReadLine();
        }
				
			
dotnetcoaching, dot net tutor

Please comment and share if you like this post and tell us about how we can enhance our posts. Thanks.