How to Reverse a String in C#

How to Reverse a String in C# with Different Mechanisms:

In this article, I am going to discuss How to Reverse a String in C# with and without using built-in Methods. Please read our previous article where we discussed Character Occurrence in a String in C# program with some examples. As part of this article, we are going to use the following three approaches to reverse a string C#.

  1. Using For Loop
  2. Using For Each Loop
  3. Using the built-in Reverse method of Array class
Program Description:

Here, we will take the input as a string from the user and then we will convert that string in reverse order as shown in the below image.

How to Reverse a String in C# with Different Mechanisms:

Reverse a String in C# without using Built-in method:

In the below program, we are using for loop to reverse a string.

using System;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a String : ");
            string originalString = Console.ReadLine();
            string reverseString = string.Empty;
            for (int i = originalString.Length - 1; i >= 0; i--)
            {
                reverseString += originalString[i];
            }
            Console.Write($"Reverse String is : {reverseString} ");
            Console.ReadLine();
        }      
    }
}

Output:

Reverse a String in C# without using Built-in method

Using the for-each loop to reverse a string in C#:

In the following example, we use for each loop to reverse a string in C#.

using System;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a String : ");
            string originalString = Console.ReadLine();
            string reverseString = string.Empty;
            foreach (char c in originalString)
            {
                reverseString = c + reverseString;
            }
            Console.Write($"Reverse String is : {reverseString} ");
            Console.ReadLine();
        }      
    }
}
Reverse a String Using in-built Reverse Method in C#:

In the following example, we use the built-in Reverse method of the Array class to reverse a string.

using System;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a String : ");
            string originalString = Console.ReadLine();

            char[] stringArray = originalString.ToCharArray();
            Array.Reverse(stringArray);
            string reverseString = new string(stringArray);
            
            Console.Write($"Reverse String is : {reverseString} ");
            Console.ReadLine();
        }      
    }
}

In the next article, I am going to discuss how to reverse each word in a given string in C# using different mechanisms. I hope now you understood how to reverse a string with and without using any built-in method in C#.

Leave a Reply

Your email address will not be published.