Write a C# Sharp program to compare the last names of two people. It then lists them in alphabetical order

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

Write a C# Sharp program to compare the last names of two people. It then lists them in alphabetical order. 
Expected Output :

Sorted alphabetically by last name:                                              
Michel Jhonson                                                                   
John Peterson  

الأجوبة

using System;
using System.Globalization;
public class Example24
{
   public static void Main()
   {
      string name1 = "John Peterson";
      string name2 = "Michel Jhonson";
      // Get position of space character.
      int index1 = name1.IndexOf(" ");
      index1 = index1 < 0 ? 0 : index1--;
      int index2 = name2.IndexOf(" ");
      index2 = index2 < 0 ? 0 : index2--;
      int length = Math.Max(name1.Length, name2.Length);
      Console.WriteLine("Sorted alphabetically by last name:");
      if (String.Compare(name1, index1, name2, index2, length, 
                         new CultureInfo("en-US"), CompareOptions.IgnoreCase) < 0)
         Console.WriteLine("{0}\n{1}", name1, name2); 
      else
         Console.WriteLine("{0}\n{1}", name2, name1); 
   }
}
هل كان المحتوى مفيد؟

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

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