How to reverse a string in C#

There are several ways to reverse a string in C#. Here are three common methods:

  • Using an array of characters:
string input = "Hello, World!";
char[] charArray = input.ToCharArray();
Array.Reverse(charArray);
string reversed = new string(charArray);
Console.WriteLine(reversed);

In this method, you convert the string to a character array using the ToCharArray() method, then use the Array.Reverse() method to reverse the character array, and finally create a new string from the reversed character array.

  • Using a StringBuilder:
using System.Text;

string input = "Hello, World!";
StringBuilder sb = new StringBuilder(input.Length);

for (int i = input.Length - 1; i >= 0; i--)
{
    sb.Append(input[i]);
}

string reversed = sb.ToString();
Console.WriteLine(reversed);

In this method, you create a new StringBuilder with the same length as the input string, then iterate through the input string in reverse order and append each character to the StringBuilder. Finally, you convert the StringBuilder to a string using the ToString() method.

  • Using LINQ:
using System.Linq;

string input = "Hello, World!";
string reversed = new string(input.Reverse().ToArray());
Console.WriteLine(reversed);

In this method, you use the Reverse() extension method from LINQ to reverse the input string, then convert the resulting IEnumerable<char> to an array and create a new string from the array.

Choose the method that best suits your needs and performance requirements. The first method using a character array is often considered the most efficient, while the LINQ method is the most concise.

  1. C# reverse string example:

    string input = "Reverse this string";
    string reversed = new string(input.Reverse().ToArray());
    // Result: "gnirts siht esreveR"
    
  2. Using Array.Reverse to reverse a string in C#:

    char[] charArray = input.ToCharArray();
    Array.Reverse(charArray);
    string reversed = new string(charArray);
    // Result: "gnirts siht esreveR"
    
  3. C# reverse string without using Array.Reverse:

    string reversed = string.Concat(input.Reverse());
    // Result: "gnirts siht esreveR"
    
  4. StringBuilder reverse string in C#:

    StringBuilder sb = new StringBuilder(input);
    sb = sb.Reverse();
    string reversed = sb.ToString();
    // Result: "gnirts siht esreveR"
    
  5. Recursive string reversal in C#:

    string reversed = ReverseStringRecursive(input);
    
    static string ReverseStringRecursive(string str)
    {
        if (string.IsNullOrEmpty(str))
            return str;
        return ReverseStringRecursive(str.Substring(1)) + str[0];
    }
    // Result: "gnirts siht esreveR"
    
  6. Reverse characters in a string C#:

    string reversed = string.Join("", input.ToCharArray().Reverse());
    // Result: "gnirts siht esreveR"
    
  7. LINQ method to reverse a string in C#:

    string reversed = new string(input.ToCharArray().Reverse().ToArray());
    // Result: "gnirts siht esreveR"
    
  8. In-place string reversal in C#:

    char[] charArray = input.ToCharArray();
    int left = 0, right = charArray.Length - 1;
    while (left < right)
    {
        char temp = charArray[left];
        charArray[left] = charArray[right];
        charArray[right] = temp;
        left++;
        right--;
    }
    string reversed = new string(charArray);
    // Result: "gnirts siht esreveR"