Programming Course in C# ¡Free!

Function WriteTitle

 Saturday, April 06, 2013 published by Exercises C#
Proposed exercise

Create a 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).

Output



Solution


using System;
public class FunctionWriteTiTle
{
public static void WriteTitle(string text)
{
int numOfSpaces =( 80 - text.Length * 2) / 2;
text = text.ToUpper();

int i = 0;

for(; i < numOfSpaces; i++)
Console.Write(" ");

for(i = 0; i < text.Length * 2 - 1; i++)
Console.Write("-");

Console.WriteLine();

for(i = 0; i < numOfSpaces; i++)
Console.Write(" ");

for(i = 0; i < text.Length; i++)
Console.Write(text[i] + " ");

Console.WriteLine();

for(i = 0; i < numOfSpaces; i++)
Console.Write(" ");

for(i = 0; i < text.Length * 2 - 1; i++)
Console.Write("-");

Console.WriteLine();
}   

public static void Main()
{
WriteTitle("Welcome!");
}
}