Ejercicio
Función escritura centrada
Objetivo
Cree una función para escribir centrado en pantalla el texto que se indica como parámetro (suponiendo un ancho de pantalla de 80 caracteres):
WriteCentered("¡Hola!");
Código de Ejemplo
// Importing the System namespace to access basic system functions
using System;
class Program
{
// Function to write the given text centered on the screen
public static void WriteCentered(string text)
{
// Define the screen width as 80 characters
int screenWidth = 80;
// Calculate the padding required on the left side to center the text
int padding = (screenWidth - text.Length) / 2;
// Write the text with the calculated padding before it, effectively centering it
Console.WriteLine(new string(' ', padding) + text);
}
// Main method to call the WriteCentered function and display the result
public static void Main()
{
// Call the WriteCentered function with the text "Hello!"
// This will print the text centered on a screen of 80 characters width
WriteCentered("Hello!");
}
}