How to Use Reverse String in C#: Using Array, For Loop & Reverse

Explore multiple ways to reverse a string in C#, including using Array.Reverse, a for loop, and built-in methods, with practical code examples.

Why Reverse a String?

Reversing a string is a common interview question and practical exercise that helps you understand how strings, arrays, and character manipulation work in C#.

Using Array.Reverse

One of the simplest approaches is converting the string to a character array using ToCharArray(), reversing it with Array.Reverse(), and then reconstructing the string with the new string() constructor.

Using a For Loop

A manual approach involves looping through the string from the last character to the first, appending each character to a new string or StringBuilder, giving you full control over the process.

Using Built-in LINQ Methods

C# also allows reversing a string concisely using LINQ's Reverse() method combined with string.Concat(), offering a compact, readable one-line solution for experienced developers.

Conclusion: Knowing multiple ways to reverse a string in C# not only helps in interviews but also deepens your understanding of arrays, loops, and the powerful LINQ library available in the .NET ecosystem.

Three Approaches in Code

using System;
using System.Linq;
using System.Text;

static string ReverseWithArray(string input)
{
    char[] chars = input.ToCharArray();
    Array.Reverse(chars);
    return new string(chars);
}

static string ReverseWithLoop(string input)
{
    var sb = new StringBuilder(input.Length);
    for (int i = input.Length - 1; i >= 0; i--)
    {
        sb.Append(input[i]);
    }
    return sb.ToString();
}

static string ReverseWithLinq(string input) => string.Concat(input.Reverse());

Console.WriteLine(ReverseWithArray("Hello"));  // olleH
Console.WriteLine(ReverseWithLoop("CSharp"));  // prahSC
Console.WriteLine(ReverseWithLinq("dotnet"));  // tentod

The array method is usually the best everyday choice: it is short, clear and fast. The loop is what interviewers want when they say "without built-in methods". LINQ is the most compact but creates extra objects along the way, so it is a weaker pick for very long strings or tight loops.

The Two-Pointer Swap

Another interview favourite swaps characters from both ends, moving inward until the pointers meet. It touches each character once and needs no extra buffer beyond the character array.

static string ReverseInPlace(string input)
{
    char[] c = input.ToCharArray();
    for (int left = 0, right = c.Length - 1; left < right; left++, right--)
    {
        (c[left], c[right]) = (c[right], c[left]);   // tuple swap
    }
    return new string(c);
}

The loop runs only half as many times as the string length, because each pass fixes two positions at once. For the word "level", the swaps are l with l, then e with e, and the middle "v" is left alone, so the result equals the input. This is exactly how a palindrome check can be written: reverse the string, or compare the two pointers, and see if every pair matches.

Remember that C# strings are immutable. Every approach here builds a new string; none of them change the original variable. That is why each method returns a value instead of modifying its parameter.

Handling Emojis and Accented Letters

A C# char is a UTF-16 code unit, not always a full visible character. Emojis use two units, and some accented letters are a base letter plus a combining mark. Reversing char by char splits them and produces broken symbols. When the text may contain such characters, reverse by text elements instead. A text element is what a reader sees as one character, and .NET can split a string into them for you. The method below collects each element, reverses the list and joins it back together, so an emoji or an accented letter stays whole:

using System.Collections.Generic;
using System.Globalization;

static string ReverseTextElements(string input)
{
    var parts = new List<string>();
    TextElementEnumerator e = StringInfo.GetTextElementEnumerator(input);
    while (e.MoveNext())
    {
        parts.Add((string)e.Current);
    }
    parts.Reverse();
    return string.Concat(parts);
}

Mistakes to Avoid

  • Building the result with += in a loop. Each addition creates a new string, which gets slow for long input. Use StringBuilder or a char array.
  • Ignoring null. Calling ToCharArray() on a null reference throws; check with string.IsNullOrEmpty first if null is possible.
  • Off-by-one loops. Starting at input.Length instead of input.Length - 1 throws an IndexOutOfRangeException.
  • Assuming every char is a letter when the input comes from users in many languages.

Quick rule: use Array.Reverse for normal code, the loop or two-pointer version for interviews, and text elements when real-world Unicode text is involved.

Ready to Master C# Programming Course?

Join Uncodemy's hands-on training program with expert mentors and real-world projects.