Q:

Write a C# program to read a string through the keyboard and sort it using bubble sort

0

Write a C# program to read a string through the keyboard and sort it using bubble sort

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 : 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a C# program to sort a string array in ascen... >>
<< Write a C# program to find maximum occurring chara...