0
0
CsharpProgramBeginner · 2 min read

C# Program to Reverse Words in a String

In C#, you can reverse words in a string by splitting it with string.Split(' '), reversing the array with Array.Reverse(), and joining back with string.Join(' ', words).
📋

Examples

Inputhello world
Outputworld hello
InputC# is fun
Outputfun is C#
Input
Output
🧠

How to Think About It

To reverse words in a string, first split the string into individual words using spaces as separators. Then reverse the order of these words. Finally, join the reversed words back into a single string separated by spaces.
📐

Algorithm

1
Get the input string.
2
Split the string into words using space as the separator.
3
Reverse the order of the words.
4
Join the reversed words into a single string with spaces.
5
Return or print the resulting string.
💻

Code

csharp
using System;

class Program {
    static void Main() {
        string input = "C# is fun";
        string[] words = input.Split(' ');
        Array.Reverse(words);
        string reversed = string.Join(' ', words);
        Console.WriteLine(reversed);
    }
}
Output
fun is C#
🔍

Dry Run

Let's trace the input "C# is fun" through the code.

1

Split the string

input.Split(' ') results in ["C#", "is", "fun"]

2

Reverse the array

Array.Reverse changes the array to ["fun", "is", "C#"]

3

Join the words

string.Join(' ', words) results in "fun is C#"

StepWords Array
After Split["C#", "is", "fun"]
After Reverse["fun", "is", "C#"]
After Join"fun is C#"
💡

Why This Works

Step 1: Splitting the string

Using Split(' ') breaks the string into words wherever there is a space.

Step 2: Reversing the words

The Array.Reverse() method reverses the order of the words in the array.

Step 3: Joining the words

Finally, string.Join(' ', words) combines the reversed words back into a single string separated by spaces.

🔄

Alternative Approaches

Using LINQ Reverse
csharp
using System;
using System.Linq;

class Program {
    static void Main() {
        string input = "C# is fun";
        string reversed = string.Join(' ', input.Split(' ').Reverse());
        Console.WriteLine(reversed);
    }
}
This method uses LINQ's <code>Reverse()</code> for a concise one-liner but requires <code>using System.Linq;</code>.
Manual loop reversal
csharp
using System;

class Program {
    static void Main() {
        string input = "C# is fun";
        string[] words = input.Split(' ');
        string reversed = "";
        for (int i = words.Length - 1; i >= 0; i--) {
            reversed += words[i] + (i > 0 ? " " : "");
        }
        Console.WriteLine(reversed);
    }
}
This approach manually builds the reversed string with a loop, which is more verbose but shows the reversal logic clearly.

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

Time Complexity

Splitting the string and reversing the array both take linear time proportional to the number of words, so overall time is O(n).

Space Complexity

Extra space is needed to store the array of words, so space complexity is O(n). The reversal is done in-place on the array.

Which Approach is Fastest?

Using Array.Reverse is efficient and simple. LINQ's Reverse() is concise but may have slight overhead. Manual loops are less readable and more error-prone.

ApproachTimeSpaceBest For
Array.ReverseO(n)O(n)Simple and efficient reversal
LINQ ReverseO(n)O(n)Concise code with LINQ
Manual loopO(n)O(n)Understanding reversal logic explicitly
💡
Use string.Split and string.Join together with Array.Reverse for a simple and efficient solution.
⚠️
Beginners often forget to join the reversed words back into a string, leaving an array instead of a string output.