Reverse String in C#

Reversing a string in C# means changing the order of characters in a given string. In C#, there are several ways to reverse a string, each with its own advantages and disadvantages.

Using the built-in string method

The simplest way to reverse a string in C# is to use the built-in string method called Reverse(). This method returns a new string with the characters reversed.

using System; public class HelloWorld { public static void Main(string[] args) { string str = "hello world"; char[] charArray = str.ToCharArray(); Array.Reverse(charArray); string reversedStr = new string(charArray); Console.WriteLine(reversedStr); } }
//Output: dlrow olleh

This method first converts the string into a character array using the ToCharArray() method. Then, it calls the static Array.Reverse() method to reverse the array of characters. Finally, it converts the reversed character array back to a string using the string constructor that takes a character array as its argument.

Using a loop

Another way to reverse a string in C# is to use a loop to iterate through the string and build a new string with the characters in reverse order.

using System; public class HelloWorld { public static void Main(string[] args) { string str = "hello world"; string reversedStr = ""; for (int i = str.Length - 1; i >= 0; i--) { reversedStr += str[i]; } Console.WriteLine(reversedStr); } }
//Output: dlrow olleh

This method initializes an empty string called reversedStr and then iterates through the original string str in reverse order using a for loop. It builds the new string by appending each character of the original string to the beginning of the reversedStr string.

Using recursion

Another way to reverse a string in C# is to use recursion. This method is not very efficient, but it is an interesting example of how recursion can be used to solve simple problems.

using System; public class HelloWorld { static string Reverse(string str) { if (str.Length <= 1) { return str; } else { return Reverse(str.Substring(1)) + str[0]; } } public static void Main(string[] args) { string str = "hello world"; string reversedStr = Reverse(str); Console.WriteLine(reversedStr); } }
//Output: dlrow olleh

This method defines a function called Reverse() that takes a string as its argument and returns a reversed string. If the length of the string is 1 or less, the function returns the original string. Otherwise, it recursively calls itself with the substring of the original string starting at index 1 and adds the first character of the original string to the end.

Using LINQ


C# Program to Reverse a Number/String without using Reverse() Method

Finally, you can use the LINQ (Language Integrated Query) library to reverse a string in C#.

string str = "hello world"; string reversedStr = new string(str.Reverse().ToArray()); Console.WriteLine(reversedStr);
//Output: dlrow olleh

This method uses the Reverse() extension method provided by the LINQ library to reverse the characters in the string. The ToArray() method is called to convert the reversed characters into an array, which is then used to create a new string using the string constructor.