belongs to collection: All String programs in C# with examples
Write a C# program to sort a string array in ascending order
I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..
using System; public class Exercise11 { public static void Main() { string str; char[] arr1; char ch; int i, j, length; Console.Write("Enter the string : "); str = Console.ReadLine(); length = str.Length; arr1 = str.ToCharArray(0, length); for (i = 1; i < length; i++) for (j = 0; j < length - i; j++) if (arr1[j] > arr1[j + 1]) { ch = arr1[j]; arr1[j] = arr1[j + 1]; arr1[j + 1] = ch; } Console.Write("After sorting the string appears like : \n"); foreach (char c in arr1) { ch = c; Console.Write("{0} ", ch); } Console.ReadLine(); } }
Result:
Enter the string : techstudy
After sorting the string appears like :
c d e h s t t u y
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..
Result:
Enter the string : techstudy
After sorting the string appears like :
c d e h s t t u y
need an explanation for this answer? contact us directly to get an explanation for this answer