Snippets Zone

Program to reverse a string in C#

By Anoop Kumar Sharma    1985  02-July-2021

This snippets shows How to reverse a string in C#


In this snippet, we will learn How to reverse a string in C#. Reversing a string is one of the basic programming questions asked by interviewer during the interview or in the exam.

Program to reverse a string in C#:

static void Main(string[] args)
{
            Console.WriteLine("Enter a string which you want to reverse");
            //Variable to strore the input string from the user/command prompt
            string inputString = Console.ReadLine();
            //Variable to reverse the string
            string reverseString = "";
            //Reverse the string
            for (int i = inputString.Length - 1; i >= 0; i--)
            {
                reverseString += inputString[i];
            }
            //Showing the output on the Console/Command prompt
            Console.WriteLine("Reverse of {0} is {1}", inputString, reverseString);
            //Console.ReadLine() to hold the screen after the execution of the program
            Console.ReadLine();
}

Preview:

I hope this program will help you in your interview/exam.