Reverse A String In C# With And Without 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. 

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));  
}

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. 

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

 Reverse String Without Inbuilt Function And Using Inbuilt Function