0
0
CsharpProgramBeginner · 2 min read

C# Program to Reverse a String with Output and Explanation

You can reverse a string in C# using new string(input.Reverse().ToArray()) or by looping through the string backwards and building a new one.
📋

Examples

Inputhello
Outputolleh
InputCSharp
OutputprahSC
Input
Output
🧠

How to Think About It

To reverse a string, think of reading the characters from the end to the start and putting them in a new string. You can do this by looping backward or using built-in functions that handle this for you.
📐

Algorithm

1
Get the input string.
2
Create a new empty string or character array to hold reversed characters.
3
Loop through the input string from the last character to the first.
4
Add each character to the new string or array.
5
Return or print the new reversed string.
💻

Code

csharp
using System;
using System.Linq;

class Program {
    static void Main() {
        string input = "hello";
        string reversed = new string(input.Reverse().ToArray());
        Console.WriteLine(reversed);
    }
}
Output
olleh
🔍

Dry Run

Let's trace the input "hello" through the code that reverses it using the built-in Reverse method.

1

Input string

input = "hello"

2

Reverse characters

input.Reverse() produces sequence: 'o', 'l', 'l', 'e', 'h'

3

Convert to array

ToArray() creates char array ['o', 'l', 'l', 'e', 'h']

4

Create new string

new string(...) becomes "olleh"

5

Print output

Console prints: olleh

IterationCharacter
1o
2l
3l
4e
5h
💡

Why This Works

Step 1: Using Reverse() method

The Reverse() method returns characters from the string in reverse order as a sequence.

Step 2: Converting to array

We convert the reversed sequence to a character array with ToArray() because new string() needs an array.

Step 3: Creating new string

The new string() constructor builds a new string from the reversed characters, giving the reversed string.

🔄

Alternative Approaches

Using a for loop
csharp
using System;
class Program {
    static void Main() {
        string input = "hello";
        string reversed = "";
        for (int i = input.Length - 1; i >= 0; i--) {
            reversed += input[i];
        }
        Console.WriteLine(reversed);
    }
}
This method is simple but less efficient because string concatenation in a loop creates many temporary strings.
Using StringBuilder
csharp
using System;
using System.Text;
class Program {
    static void Main() {
        string input = "hello";
        var sb = new StringBuilder();
        for (int i = input.Length - 1; i >= 0; i--) {
            sb.Append(input[i]);
        }
        Console.WriteLine(sb.ToString());
    }
}
Using StringBuilder is more efficient for longer strings because it avoids creating many temporary strings.

Complexity: O(n) time, O(n) space

Time Complexity

The reversal requires visiting each character once, so it takes linear time proportional to the string length.

Space Complexity

A new string or array is created to hold reversed characters, so space grows linearly with input size.

Which Approach is Fastest?

Using Reverse() with ToArray() is concise and efficient; StringBuilder is better for very long strings when using loops.

ApproachTimeSpaceBest For
Reverse() + ToArray()O(n)O(n)Quick and readable for most cases
For loop with string concatenationO(n²)O(n)Simple but slow for long strings
For loop with StringBuilderO(n)O(n)Efficient for long strings with manual control
💡
Use input.Reverse().ToArray() with new string() for a quick and clean string reversal.
⚠️
Beginners often try to reverse strings by modifying the original string directly, but strings are immutable in C#.