Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to Reverse words in a string
In C#, reversing words in a string means reversing each individual word while keeping the words in their original positions. For example, "Hello World" becomes "olleH dlroW" where each word is reversed but the word order remains the same.
Syntax
Using LINQ with Split(), Select(), and Reverse() methods −
string result = string.Join(" ", str.Split(' ').Select(word => new string(word.Reverse().ToArray())));
Using a traditional loop approach −
string[] words = str.Split(' ');
for (int i = 0; i
Using LINQ Methods
The most concise approach uses LINQ methods to split the string into words, reverse each word, and join them back together −
using System;
using System.Linq;
class Demo {
static void Main() {
// original string
string str = "Hello World";
Console.WriteLine("Original: " + str);
// reverse each word using LINQ
string result = string.Join(" ", str.Split(' ').Select(s => new string(s.Reverse().ToArray())));
Console.WriteLine("Reversed: " + result);
}
}
The output of the above code is −
Original: Hello World
Reversed: olleH dlroW
Using Traditional Loop Approach
For better readability and understanding, you can also reverse words using a traditional loop approach −
using System;
class Demo {
static string ReverseWord(string word) {
char[] charArray = word.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
static void Main() {
string str = "Programming in CSharp";
Console.WriteLine("Original: " + str);
string[] words = str.Split(' ');
for (int i = 0; i
The output of the above code is −
Original: Programming in CSharp
Reversed: gnimmargorP ni prahSC
Using StringBuilder for Better Performance
For larger strings or performance-critical applications, using StringBuilder can be more efficient −
using System;
using System.Text;
class Demo {
static string ReverseWordsWithStringBuilder(string str) {
string[] words = str.Split(' ');
StringBuilder result = new StringBuilder();
for (int i = 0; i = 0; j--) {
reversedWord.Append(words[i][j]);
}
if (i > 0) result.Append(" ");
result.Append(reversedWord.ToString());
}
return result.ToString();
}
static void Main() {
string str = "Learn CSharp Programming";
Console.WriteLine("Original: " + str);
Console.WriteLine("Reversed: " + ReverseWordsWithStringBuilder(str));
}
}
The output of the above code is −
Original: Learn CSharp Programming
Reversed: nraeL prahSC gnimmargorP
Comparison
| Approach | Advantages | Best Use Case |
|---|---|---|
| LINQ Methods | Concise, functional style | Small to medium strings, readability priority |
| Traditional Loop | Easy to understand, no LINQ dependency | Educational purposes, simple logic |
| StringBuilder | Better performance for large strings | Performance-critical applications |
Conclusion
Reversing words in a string in C# can be accomplished using LINQ methods for concise code, traditional loops for clarity, or StringBuilder for performance. The LINQ approach with Split(), Select(), and Reverse() is typically the most elegant solution for most scenarios.
