Format Array display in C #?

4

I have a string Array.

string[] nomes =
{
    "Maria",
    "João",
    "Francisco",
    "José",
    "Antonio",
    "Osvaldo",
    "Francimar",
    "Cirineu"
};

I would like to display them exactly like this in the Console

Maria   João    Francisco José
Antonio Osvaldo Francimar Cirineu
    
asked by anonymous 19.11.2018 / 22:54

1 answer

2

Here is the code, and its operation explained in the comments

using System;

public class Program
{
    public static void Main()
    {
        string[] nomes =
        {
            "Maria",
            "João",
            "Francisco",
            "José",
            "Antonio",
            "Osvaldo",
            "Francimar",
            "Cirineu"
        };

        //Pecorre todos os nomes
        for (int key = 0; key < nomes.Length; ++key){

            //Escreve o nome
            Console.Write(nomes[key]);

            //Acrescenta espaço na frente do nome par ao alinhamento
            for (int i = 0; i <= 10 - nomes[key].Length; ++i){
                Console.Write(" ");
            }

            //Faz a quebra no quarto nome
            if((key + 1) % 4 == 0){
                Console.Write("\n");
            }
        }
    }
}
    
19.11.2018 / 23:39