Function Return Value For Main - C# Programming Exercise

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 in the command line. If the user doesn't specify any text, your program will display an error message and return a value of 1 to the operating system. This exercise is excellent for practicing how to read command line arguments in C# and how to handle input errors. By using the WriteTitle function, you can customize the presentation of the text on the screen, and with the control flow logic, you can manage different cases in the program's execution.

With this exercise, you'll learn how to manage command line arguments and errors in C#, thus improving the robustness and flexibility of your programs.

 Category

Functions

 Exercise

Function Return Value For Main

 Objective

Write a C# program in which you write a title (using the previous WriteTitle function) which the user will specify in command line. If no text is specified, your program will display an error message and return a value of 1 to the operatiing system.

 Write Your C# Exercise

// Importing necessary namespaces
using System;

class Program
{
    // Main method to drive the program
    public static void Main(string[] args)
    {
        // Check if any argument is passed (i.e., a title)
        if (args.Length == 0)
        {
            // If no argument is provided, show an error message and return 1
            Console.WriteLine("Error: No title specified.");
            Environment.Exit(1); // Return 1 to the operating system
        }
        else
        {
            // If an argument is provided, call the WriteTitle function with the user's title
            string title = string.Join(" ", args); // Join the arguments if there are multiple words
            WriteTitle(title); // Display the title
        }
    }

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

  •  Function MinMaxArray

    In this exercise of C#, you will create a function named MinMaxArray that receives an array of numbers and returns the minimum and maximum values using ...