Function Writetitle - C# Programming Exercise

In this exercise of C#, you will learn how to create a function named "WriteTitle" that writes a text centered on the screen, in uppercase, with extra spaces, and with a line over it and another line under it. This exercise is great for practicing how to handle strings and how to format the output in the console. The function will receive a text as a parameter and display it on the screen with the desired format: the word will be in uppercase, centered in a line of 80 columns, with hyphens before and after the text. For example, if the program is run with the text "Welcome!", the output would be: "--------------- W E L C O M E ! ---------------", adjusting the number of hyphens depending on the length of the text. This exercise is perfect for improving your skills in C# when working with strings, spacing, and text alignment.

With this exercise, you will learn how to manipulate and format text in C#, allowing you to create custom outputs and improve the presentation of your programs.

 Category

Functions

 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

// 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);
    }
}

 Share this C# exercise

 More C# Programming Exercises of Functions

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Function return value for Main

    In this exercise of C#, you will learn how to create a program in which you can use the WriteTitle function to write a title that the user will specify ...

  •  Function CountDV

    In this exercise of C#, you will learn how to create a function that calculates the amount of numeric digits and vowels a text string contains. The function...

  •  Function IsAlphabetic

    In this exercise of C#, you will learn how to create a function that tells if a character is alphabetic (from A to Z) or not. This function should be us...

  •  Function IsNumber

    In this exercise of C#, you will learn how to create a function that tells if a text string represents an integer number. This function should be used l...

  •  Function calculator, params of Main

    In this exercise of C#, you will learn how to create a program that performs mathematical operations such as addition, subtraction, multiplication, or division...

  •  Function calculator, params and return value of Main

    In this exercise of C#, you will create a program that calculates mathematical operations like addition, subtraction, multiplication, or division, by analyzing...