In this blog, we will learn how to reverse a string in C#. We will try two ways - without an inbuilt function, and using an inbuilt function. Here, we will create a simple code example for both ways and will see how to achieve this in easy steps.
Let's start with using the inbuilt function.
How to reverse a string with Inbuilt function in C#
In the below example, I have tried to reverse a string using the inbuilt function, Array.Reverse method which will reverse and print a string in the console. Here is the code.
static void Main(string[] args)
{
ReverseStringWithInbuiltMethod("CSharpCorner");
}
private static void ReverseStringWithInbuiltMethod(string stringInput)
{
// With Inbuilt Method Array.Reverse Method
char[] charArray = stringInput.ToCharArray();
Array.Reverse(charArray);
Console.WriteLine(new string(charArray));
}

One more way to reverse a string is using LINQ. First, an input string is converted into a character array using ToCharArray() and then, the Reverse() method is applied to reverse the characters.
private static void ReverseStringWithInbuiltMethod(string stringInput)
{
// using Linq
var resultstring = new string(stringInput.ToCharArray().Reverse().ToArray());
Console.WriteLine(resultstring);
Console.ReadLine();
}
How to reverse a string without Inbuilt function in C#
Here, we will reverse a string without using the inbuilt function. There are two ways to perform this - using a While loop and using a For loop. Here are the examples of both.
static void Main(string[] args)
{
ReverseStringWithoutInbuiltMethod("CSHARPCORNER");
}
Using While Loop
private static void ReverseStringWithoutInbuiltMethod(string stringInput)
{
// Reverse using While loop
string reversestring = "";
int length;
length = stringInput.Length - 1;
while (length >= 0)
{
reversestring = reversestring + stringInput[length];
length--;
}
Console.WriteLine(reversestring);
Console.ReadLine();
}
In the above example,
- First, the length of the input string is counted.
- Based on the length, the While loop runs.
- It starts assigning characters one by one from the last index of the character to a reversestring variable.
- Decrement the length by one every time the loop runs.
Using For Loop
private static void ReverseStringWithoutInbuiltMethod(string stringInput)
{
// Reverse using for loop
//Convert input string to char array and loop through
char[] stringArray = stringInput.ToCharArray();
string reverse = String.Empty;
for (int i = stringArray.Length - 1; i >= 0; i--)
{
reverse += stringArray[i];
}
Console.WriteLine(reverse);
Console.ReadLine();
}
In the above example,
- First, the input string is converted into character array.
- Based on the char array length, it assigns the characters one by one from the last index of character to reverse variable.
- It decrements the i variable every time loop runs.
Output


Eduard De JongPosted May 25, 2022, 2:52 PM
Update: My use of .ToCharArray() wasn't even needed. System.Linq.Enumerable.Reverse() can be applied directly on System.String itself.
Eduard De JongPosted May 25, 2022, 2:14 PM
Same thing in PowerShell: [string]::Concat([System.Linq.Enumerable]::Reverse('abcdefghijklmnopqrstuvwxyz'.ToCharArray()));
Eduard De JongPosted May 25, 2022, 2:11 PM
Very easy way using System.Linq.Enumerable's Reverse() extension method: string.Concat("abcdefghijklmnopqrstuvwxyz".ToCharArray().Reverse());
Otto AkamaPosted Nov 10, 2021, 7:41 AM
C# engineers find it hard to create an inbuilt string method like String.Reverse(nameOfString);
Dharmendra Kumar PanditPosted Feb 13, 2020, 11:53 PM
NIce Article for string reverse.........
Akshay DalviPosted Oct 23, 2019, 11:22 PM
Is it possible to reverse a string variable without converting to charArray as shown in while loop?
Stephen BarrettPosted Sep 22, 2019, 6:47 PM
Is it possible to do this without looping?
Laxmidhar SahooPosted Apr 20, 2019, 1:15 AM
Thanks for a usefull article
Madan ShekarPosted Apr 18, 2019, 4:21 AM
Most asked question in interviews ....... thanks for sharing