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

  • برمجة سي شارب
  • برمجة

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

Test Data :
Input the string : this is a string
Expected Output :

After sorting the string appears like : 
a g h i i i n r s s s t t 

الأجوبة

using System;  
public class Exercise11  
{  
    public static void Main() 
{
  string str;
  char[] arr1;
  char ch;
  int i,j,l;
      Console.Write("\n\nSort a string array in ascending order :\n");
      Console.Write("--------------------------------------------\n");  
      Console.Write("Input the string : ");
      str = Console.ReadLine();	
      l=str.Length;
      arr1 = str.ToCharArray(0, l);
   for(i=1;i<l;i++)
    for(j=0;j<l-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.WriteLine("\n");
   }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...