Function Write Underlined - C# Programming Exercise

In this C# exercise, you are asked to write a function that displays the given text centered on the screen (assuming a screen width of 80 characters) and then underlines it by writing several hyphens below that word. The code should look something like this:


WriteUnderlined("Hello!");

The WriteUnderlined function must accept a string parameter (in this case, the text "Hello!") and display it centered on the screen, then underline it by writing hyphens on a new line below the text. This exercise is useful for practicing how to manipulate strings and work with text alignment in the C# console, as well as for understanding how to generate more complex output formats, such as underlining, in the console.

 Category

Functions

 Exercise

Function Write Underlined

 Objective

Write a C# function able to write centered on screen the text that is indicated as a parameter (supposing a screen width of 80 characters) and then underline it (writing several hyphens under that word):

WriteUnderlined("Hello!");

 Write Your C# Exercise

// Importing the System namespace to access basic system functions
using System;

class Program
{
    // Function to write the given text centered on the screen and then underline it
    public static void WriteUnderlined(string text)
    {
        // Define the screen width as 80 characters
        int screenWidth = 80;

        // Calculate the padding required on the left side to center the text
        int padding = (screenWidth - text.Length) / 2;

        // Write the text with the calculated padding before it, effectively centering it
        Console.WriteLine(new string(' ', padding) + text);

        // After the text, print a line of hyphens, matching the length of the text
        // The number of hyphens is equal to the length of the text
        Console.WriteLine(new string('-', text.Length));
    }

    // Main method to call the WriteUnderlined function and display the result
    public static void Main()
    {
        // Call the WriteUnderlined function with the text "Hello!"
        // This will print the text centered on a screen of 80 characters width, and underline it
        WriteUnderlined("Hello!");
    }
}

 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 sum of array

    In this C# exercise, you are asked to write a program to calculate the sum of the elements in an array. The Main function should look like the following:<...

  •  Function double

    In this C# exercise, you are asked to write a function named "Double" to calculate and return an integer number doubled. For example, when calling the Double(7...

  •  Function Double reference parameter

    In this C# exercise, you are asked to write a function named "Double" that calculates the double of an integer number and modifies the data passed as an argument. Thi...

  •  Function swap reference parameters

    In this C# exercise, you are asked to write a function named "Swap" that swaps the values of two integer numbers, which are passed by reference. This is done b...

  •  Function power local variables

    In this C# exercise, you are asked to write a function named "Power" that calculates the result of raising an integer number to another positive integer number. The ...

  •  Function recursive power

    In this C# exercise, you are asked to write a function that calculates the result of raising one integer to another integer using recursion. For example, raising 5 to...