-4

I'm stucked to figurer out how can I make a program to change the order of the name in array. It's expect that the program show first the last name and after the first name and the rest of the name must be abreviated.

    static void Main(string[] args)
    {
        string[] names = { "Paul Page Load Wood", "Michael Kraiser Unter", "Mia Rock Spark" };
        /*Present the names 
        names[1] = "Wood, Paul P. l.";
        names[2] = "Unter, Michael K.";
        names[3] = "Spark, Mia R."*/

    }

Can you please help.

Thank you

Here's what I have so far:

static void Main(string[] args)
{
    string[] names = { "Paul Page Load Wood", "Michael Kraiser Unter", "Mia Rock Spark" };
    int i = 0;
    foreach (string name in names)
    {
        string[] eachName = name.Split(' '); // I was advised to no use .split
        for (int j = 0; j < eachName.Length; j++)
        {
            Console.WriteLine("{0} {1}", j, eachName[j]);
        }
        i++;
        Console.WriteLine();
    }
}
Paul Abbott
  • 7,065
  • 3
  • 27
  • 45
  • 1
    You should write what you've tried so far. We're here to help you, not make your homework. – Fabricio Koch Nov 16 '16 at 19:41
  • 1
    [Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it.](http://stackoverflow.com/help/on-topic) – Paul Abbott Nov 16 '16 at 19:41
  • 1
    Possible duplicate of [Simple way to parse a person's name into its component parts?](http://stackoverflow.com/questions/103422/simple-way-to-parse-a-persons-name-into-its-component-parts) – Kritner Nov 16 '16 at 19:41
  • 1
    Why not `Spark, Mia R.` ? – Paul Abbott Nov 16 '16 at 19:43
  • Correct, must be Spark, Mia R. – Pedro Silva Nov 16 '16 at 19:46
  • So far I have this: – Pedro Silva Nov 16 '16 at 19:46
  • static void Main(string[] args) { string[] names = { "Paul Page Load Wood", "Michael Kraiser Unter", "Mia Rock Spark" }; int i = 0; foreach (string name in names) { string[] eachName = name.Split(' '); // I was advised to no use .split for (int j = 0; j < eachName.Length; j++) { Console.WriteLine("{0} {1}",j,eachName[j]); } i++; Console.WriteLine(); } – Pedro Silva Nov 16 '16 at 19:46
  • 1
    Edit your post and put the code there, don't put it in comments @PedroSilva. – Quantic Nov 16 '16 at 19:49

1 Answers1

1

First, let's elaborate rules:

  1. One part "John" -> "John" (do nothing)
  2. Two parts "John Smith" -> "Smith, John" (last, first)
  3. Three+ parts "John Peter Jack Smith" -> "Smith, John P. J." (last, first, other in order as single letters)

Having these rules we can implement a simple reordering:

private static String ReOrderNamesParts(string name) {
  if (string.IsNullOrEmpty(name))
    return name;

  string[] parts = name.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

  if (parts.Length <= 0)
    return name;

  StringBuilder sb = new StringBuilder(parts[parts.Length - 1]);

  if (parts.Length > 2) {
    sb.Append(", ");
    sb.Append(parts[0]);
  }

  for (int i = 1; i < parts.Length - 1; ++i) {
    sb.Append(' ');
    sb.Append(parts[i].Substring(0, 1));
    sb.Append('.');
  }

  return sb.ToString();
}

And so you can put

string[] names = { "Paul Page Load Wood", "Michael Kraiser Unter", "Mia Rock Spark" };

for (int i = 0; i < names.Length; ++i)
  names[i] = ReOrderNamesParts(names[i]);

Or if you want just to print out:

Console.Write(String.Join(Environment.NewLine, 
  names.Select(name => eOrderNamesParts(names))));
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • @Pedro Silva: what's wrong with `Split` and `StringBuilder`, please? It's quite natural to *split* the initial string into its parts and then *build* from the parts reordered. – Dmitry Bychenko Nov 16 '16 at 20:35