Write a C# program to read a string through the keyboard and sort it using bubble sort
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 Stringexercise { public static void Main() { string[] arr1; string temp; int num, i, j, lenghth; Console.Write("Input number of strings :"); num = Convert.ToInt32(Console.ReadLine()); arr1 = new string[num]; Console.Write("Input {0} strings below :\n", num); for (i = 0; i < num; i++) { arr1[i] = Console.ReadLine(); } lenghth = arr1.Length; for (i = 0; i < lenghth; i++) { for (j = 0; j < lenghth - 1; j++) { if (arr1[j].CompareTo(arr1[j + 1]) > 0) { temp = arr1[j]; arr1[j] = arr1[j + 1]; arr1[j + 1] = temp; } } } Console.Write("\n\nAfter sorting the array appears like : \n"); for (i = 0; i < lenghth; i++) { Console.WriteLine(arr1[i] + " "); } Console.ReadLine(); } }
Result:
Input number of strings :3
Input 3 strings below :
C
B
A
After sorting the array appears like :
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:
Input number of strings :3
Input 3 strings below :
C
B
A
After sorting the array appears like :
A
B
C
need an explanation for this answer? contact us directly to get an explanation for this answer