How to reverse a string in C sharp

Reverse a string in C# :

In this tutorial, we will learn how to reverse a string in C sharp. The program will take one string from the user, reverse it and print out the reversed string. We will learn two different approaches to solve this problem.

Approach 1: Using array :

We can easily reverse an array using Array.Reverse() method. But we don’t have any such methods available to reverse a string.

Our idea to solve this program is to convert the string to a character array first. Then reverse the array using _Array.Reverse() _method and finally convert it back to a string. Full C# program : c sharp reverse a string using an array

using System;

namespace dotnet_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            //1
            string str1;

            //2
            Console.WriteLine(“Enter the string :);
            str1 = Console.ReadLine();

            //3
            char[] arr = str1.ToCharArray();
            Array.Reverse(arr);

            //4
            Console.WriteLine(new String(arr));
           
        }
    }
}

Explanation :

The commented numbers in the above program denote the step numbers below :

  1. Create one string variable str1. We will read the user input and store the string in this variable.

  2. Ask the user to enter the string. Read the string and store it in str1 variable using Console.ReadLine().

  3. Convert the string to a character array using ToCharArray() method. This array is stored in the arr variable. Reverse this array using Array.Reverse() method. Note that this method will reverse the array in-place. It will not return anything.

  4. Convert the array to a string using String() method and print out the result to the user.

Sample Output :

Enter the string :
hello
olleh

Enter the string :
abcdefghijklmnopqrstuvwxyz
zyxwvutsrqponmlkjihgfedcba

Enter the string :
abc123
321cba

Enter the string :
Hello World
dlroW olleH

2. Approach 2: by reading the characters :

The previous approach was simple. But, instead of converting a string to an array and converting it back to a string, we can also iterate each character of the string to build the reversed string.

Our program will iterate through the characters starting from the end to start. It will read the characters one by one and append them to a new string. After the iteration will be completed, the new string will be the reversed string.

C# program :

We can access characters of a string by using its_ index number_. The index of the characters starts at_ 0_ and ends at length - 1. For example, for the string ’hello’, the index of ’h’ is 0 and the index of ’o’ is 4.

using System;

namespace dotnet_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            //1
            string inputString;
            string revString = “”;

            //2
            Console.WriteLine(“Enter the string :);
            inputString = Console.ReadLine();

            //3
            int Size = inputString.Length - 1;
            
            //4
            while(Size >= 0)
            {
                //5
                revString = revString + inputString[Size];
                Size—;
            }

            //6
            Console.WriteLine(“The reversed string is :+revString);
        }
    }
}

c sharp reverse a string by reading the characters

Explanation :

The commented numbers in the above program denote the step numbers below :

  1. Create two string variables inputString and revString. Variable inputString is for storing the main string and revString is for storing the reversed string.

  2. Ask the user to enter a string. Read and store it in inputString.

  3. Get the size of the string using string.Length method. Note that we are subtracting 1 from the length as the last index of the character is_ length - 1_.

  4. Using one while loop, check till Size is greater than or equal to zero.

  5. Add the last character of the string to the revString variable. Also, decrement the Size by

  6. Finally, print the reversed string to the user.

Sample Output :

Enter the string :
once
The reversed string is : ecno

Enter the string :
12 34 56
The reversed string is : 65 43 21

Enter the string :
once upon a time
The reversed string is : emit a nopu ecno

Conclusion :

You can use any of the above approaches to reverse a string in C#. Instead of a_ ’while’_ loop, you can also use one ’for’ loop to build the reverse string. Try to run the programs and if you have any more idea to solve this problem, drop one comment below.