Function WriteTitle Learn programming Visual Basic (VB.net)

Lesson:

Functions


Exercise:

Function WriteTitle 11


Objetive:

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


Code:

Imports System
Public Class exercise120
    Public Shared Sub WriteTitle(ByVal text As String)
        Dim numOfSpaces As Integer = (80 - text.Length * 2) / 2
        text = text.ToUpper()

        For i As Integer = 0 To numOfSpaces - 1
            Console.Write(" ")
        Next

        For i As Integer = 0 To text.Length * 2 - 1 - 1
            Console.Write("-")
        Next

        Console.WriteLine()

        For i As Integer = 0 To numOfSpaces - 1
            Console.Write(" ")
        Next

        For i As Integer = 0 To text.Length - 1
            Console.Write(text(i) & " ")
        Next

        Console.WriteLine()

        For i As Integer = 0 To numOfSpaces - 1
            Console.Write(" ")
        Next

        For i As Integer = 0 To text.Length * 2 - 1 - 1
            Console.Write("-")
        Next

        Console.WriteLine()
    End Sub

    Public Shared Sub Main()
        WriteTitle("Welcome!")
        Console.WriteLine("Enter a text: ")
        Dim text As String = Console.ReadLine()
        WriteTitle(text)
    End Sub
End Class