Reverse A String In C# With And Without An Inbuilt Function

This blog explains how to reverse a string in C# using an inbuilt function as well as without using an inbuilt function.

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. 
 
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.
  1. static void Main(string[] args)  
  2. {  
  3.      ReverseStringWithInbuiltMethod("CSharpCorner");  
  4. }  
  5.   
  6. private static void ReverseStringWithInbuiltMethod(string stringInput)  
  7. {  
  8.        // With Inbuilt Method Array.Reverse Method  
  9.        char[] charArray = stringInput.ToCharArray();  
  10.        Array.Reverse(charArray);  
  11.        Console.WriteLine(new string(charArray));  
  12. }  
Reverse String Without Inbuilt Function And Using Inbuilt Function
 
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. 
  1. private static void ReverseStringWithInbuiltMethod(string stringInput)  
  2.  {       
  3.             // using Linq  
  4.   
  5.     var resultstring = new string(stringInput.ToCharArray().Reverse().ToArray());  
  6.     Console.WriteLine(resultstring);  
  7.   
  8.     Console.ReadLine();  
  9.   
  10.  }  

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.
  1. static void Main(string[] args)  
  2. {  
  3.      ReverseStringWithoutInbuiltMethod("CSHARPCORNER");  
  4. }  
Using While Loop
  1. private static void ReverseStringWithoutInbuiltMethod(string stringInput)  
  2. {  
  3.      // Reverse using While loop  
  4.      string reversestring = "";  
  5.      int length;  
  6.   
  7.      length = stringInput.Length - 1;  
  8.   
  9.      while (length >= 0)  
  10.      {  
  11.         reversestring = reversestring + stringInput[length];  
  12.         length--;  
  13.      }  
  14.      Console.WriteLine(reversestring);  
  15.   
  16.      Console.ReadLine();  
  17.   
  18. }  
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
  1. private static void ReverseStringWithoutInbuiltMethod(string stringInput)  
  2. {  
  3.       
  4.     // Reverse using for loop  
  5.     //Convert input string to char array and loop through  
  6.     char[] stringArray = stringInput.ToCharArray();  
  7.   
  8.     string reverse = String.Empty;  
  9.   
  10.     for (int i = stringArray.Length - 1; i >= 0; i--)  
  11.     {  
  12.         reverse += stringArray[i];  
  13.     }  
  14.   
  15.     Console.WriteLine(reverse);  
  16.   
  17.     Console.ReadLine();  
  18. }  
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
 
 Reverse String Without Inbuilt Function And Using Inbuilt Function