Exercise
Function Writetitle
Objective
Write a C# function named "WriteTitle" to write a text centered on screen, uppercase, with extra spaces and with a line over it and another line under it:
WriteTitle("Welcome!");
would write on screen (centered on 80 columns):
--------------- W E L C O M E ! ---------------
(Obviously, the number of hyphens should depend on the length of the text).
Write Your C# Exercise
C# Exercise Example
// Importing necessary namespaces
using System;
class Program
{
// Main method to drive the program
public static void Main()
{
// Calling the WriteTitle function with the text "Welcome!"
WriteTitle("Welcome!"); // Example output: The text will be centered with lines above and below
}
// Function to write the title with lines above and below
public static void WriteTitle(string text)
{
// Defining the screen width (80 columns)
int screenWidth = 80;
// Convert the text to uppercase and add extra spaces between each character
string upperText = string.Join(" ", text.ToUpper().ToCharArray());
// Calculate the number of hyphens needed for the lines
int totalLength = upperText.Length + 4; // 2 extra spaces for padding on each side
int hyphenCount = (screenWidth - totalLength) / 2;
// Create the line of hyphens
string hyphens = new string('-', hyphenCount);
// Display the line above the text
Console.WriteLine(hyphens);
// Display the text centered
Console.WriteLine($"{hyphens} {upperText} {hyphens}");
// Display the line below the text
Console.WriteLine(hyphens);
}
}