Write a C# program to sort a string array in ascending order


Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2. public class Exercise11
  3. {
  4. public static void Main()
  5. {
  6. string str;
  7. char[] arr1;
  8. char ch;
  9. int i, j, length;
  10. Console.Write("Enter the string : ");
  11. str = Console.ReadLine();
  12. length = str.Length;
  13. arr1 = str.ToCharArray(0, length);
  14.  
  15. for (i = 1; i < length; i++)
  16. for (j = 0; j < length - i; j++)
  17.  
  18. if (arr1[j] > arr1[j + 1])
  19. {
  20. ch = arr1[j];
  21. arr1[j] = arr1[j + 1];
  22. arr1[j + 1] = ch;
  23. }
  24. Console.Write("After sorting the string appears like : \n");
  25. foreach (char c in arr1)
  26. {
  27. ch = c;
  28. Console.Write("{0} ", ch);
  29. }
  30. Console.ReadLine();
  31. }
  32. }

Result

C# language techstudy

     





Related Topics




Popular Posts