Function Parameters Of Main, Reverse - C# Programming Exercise

In this exercise of C#, you will learn how to create a program named reverse that receives several words from the command line and displays them in reverse order. This exercise is useful for practicing how to manipulate strings and how to work with input from the command line in C#. The program takes the provided words, organizes them in an array or list, and then displays them in reverse order. For example, if the program is run with the words "one", "two", and "three", it will show "three two one". This type of exercise is great for learning about data manipulation in C# and how to manage user input in a console application.

Learn how to manipulate inputs from the command line and reverse their order with this hands-on exercise in C#, and enhance your console application programming skills.

 Category

Functions

 Exercise

Function Parameters Of Main, Reverse

 Objective

Write a C# program named "reverse", which receives several words in the command line and displays them in reverse order, as in this example:

reverse one two three
three two one

 Write Your C# Exercise

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

class Program
{
    // Main method where the program starts
    public static void Main(string[] args)
    {
        // Check if there are any arguments passed from the command line
        if (args.Length == 0)
        {
            Console.WriteLine("No words provided."); // If no words are provided, print a message
            return; // Exit the program
        }

        // Loop through the arguments in reverse order and print each word
        for (int i = args.Length - 1; i >= 0; i--)
        {
            Console.Write(args[i] + " "); // Print each word followed by a space
        }
        Console.WriteLine(); // Add a new line at the end
    }
}

 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 GetInt

    In this exercise of C#, you will learn how to create a function named GetInt, which displays on screen the text received as a parameter, asks the user f...

  •  Function tasks database

    In this exercise of C#, you will improve a "tasks database" program by splitting it into multiple functions. This type of exercise is great for l...

  •  Function Greatest value in a array

    In this exercise of C#, you will learn how to create a function that returns the greatest value stored in an array of real numbers, which is specified as a par...

  •  Function factorial (iterative)

    In this exercise of C#, you will learn how to create an iterative (non-recursive) function to calculate the factorial of the number specified as a parameter. T...

  •  Function WriteTitle

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

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