Q:

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

0

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

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 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 

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

total answers (1)

Write a C# program to compare (less than, greater ... >>
<< Write a C# program to read a string through the ke...