Reverse a string in C#
This post will discuss how to reverse a string in C#.
1. Using Array.Reverse() method
The idea is to convert the given string into a character array using the String.ToCharArray() method and then reverse the character array in-place using the Array.Reverse() method. Finally, convert the reversed array back to the string using the String constructor. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; public static class Extensions { public static string Reverse(this string str) { char[] chars = str.ToCharArray(); Array.Reverse(chars); return new string(chars); } } public class Example { public static void Main() { string str = "Reverse Me!!"; string reverse = str.Reverse(); Console.WriteLine(reverse); } } /* Output: !!eM esreveR */ |
2. Using Enumerable.Reverse() method
The following code example demonstrates how to use LINQ’s Reverse() method for reversing a string in C#.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; using System.Linq; public static class Extensions { public static string reverse(this string str) { return new string(str.Reverse().ToArray()); } } public class Example { public static void Main() { string str = "Reverse Me!!"; string reverse = str.reverse(); Console.WriteLine(reverse); } } /* Output: !!eM esreveR */ |
3. Naive solution
Following is another simple way to reverse a string in C#:
- Create a character array from given string using
ToCharArraymethod. - Start from the two endpoints of the given array and run the for-loop till two endpoints intersect. In each iteration of the loop, swap values present at two indexes.
- Finally, convert the character array back into a string using string constructor.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; public static class Extensions { public static string Reverse(this string str) { char[] chars = str.ToCharArray(); for (int i = 0; i < str.Length / 2; i++) { char ch = chars[i]; chars[i] = chars[str.Length-i-1]; chars[str.Length-i-1] = ch; } return new string(chars); } } public class Example { public static void Main() { string str = "Reverse Me!!"; string reverse = str.Reverse(); Console.WriteLine(reverse); } } /* Output: !!eM esreveR */ |
4. Using StringBuilder.Append() method
We can use the StringBuilder.Append() method to reverse a string in C#. The idea is to read characters from the end of the string and append each character to the StringBuilder instance. Finally, call the ToString() method to get the reversed string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using System; using System.Text; public static class Extensions { public static string reverse(this string str) { StringBuilder sb = new StringBuilder(); for (int i = str.Length - 1; i >=0 ; i--) { sb.Append(str[i]); } return sb.ToString(); } } public class Example { public static void Main() { string str = "Reverse Me!!"; string reverse = str.reverse(); Console.WriteLine(reverse); } } /* Output: !!eM esreveR */ |
That’s all about reversing a string in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)