Grupo
Funciones en C#
Objectivo
1. Cree una función llamada "WriteTitle" que acepte una cadena como parámetro.
2. Convierta la cadena a mayúsculas.
3. Añada espacios adicionales para centrar el texto en una pantalla de 80 columnas.
4. Añada una línea de guiones encima y debajo del texto.
5. Imprima el título en el formato que se muestra a continuación.
Escriba una función en C# llamada "WriteTitle" para escribir un texto centrado en la pantalla, en mayúsculas, con espacios adicionales, una línea encima y otra debajo.
Ejemplo de ejercicio en C#
Mostrar código C#
using System;
class Program
{
// Function to write a centered title with lines above and below the text
public static void WriteTitle(string text)
{
// Convert the text to uppercase
string upperText = text.ToUpper();
// Calculate the total width (80 characters) minus the length of the title
int spaces = (80 - upperText.Length) / 2;
// Create the line of hyphens based on the length of the title
string line = new string('-', upperText.Length + spaces * 2);
// Print the top line of hyphens
Console.WriteLine(line);
// Print the centered title with spaces on both sides
Console.WriteLine(new string(' ', spaces) + upperText + new string(' ', spaces));
// Print the bottom line of hyphens
Console.WriteLine(line);
}
// Main method to test the WriteTitle function
public static void Main()
{
// Test the WriteTitle function with the text "Welcome!"
WriteTitle("Welcome!"); // This will display the centered title with lines
}
}
Output
----------------------- WELCOME! ------------------------
Código de ejemplo copiado
Comparte este ejercicio de C#