programming-examples/c-sharp/Strings/C# Sharp program to compare the last names of two people.cs

23 lines
830 B
C#
Raw Normal View History

2019-11-15 12:59:38 +01:00
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(" ");
index1 = index1 < 0 ? 0 : index1--;
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);
}
}