Home
C#
String Reverse
Updated Sep 17, 2024
Dot Net Perls
Reverse string. A string in C# can be reversed. A reversal method can be implemented in several ways. Some ways are faster than others.
Method notes. Some methods use more lines of code—they are more complex to understand. We review and time some string reversal methods in .NET.
An example. This solution requires just 3 lines of code. The method shown here is based on ToCharArray, which can help performance. It is static because it stores no state.
String ToCharArray
Info ReverseString receives a string parameter—this is the string in which you wish to reverse the order of the letters.
Note The method calls Array.Reverse to modify the order of the chars. Reverse cannot be used on a string directly.
Array.Reverse
Note 2 The method copies to an array using ToCharArray. It returns a string with the new string constructor.
char Array
String Constructor
using System; class Program { static string ReverseString(string s) { // Convert to char array, then call Array.Reverse. // ... Finally use string constructor on array. char[] array = s.ToCharArray(); Array.Reverse(array); return new string(array); } static void Main() { Console.WriteLine(ReverseString("BIRD")); Console.WriteLine(ReverseString("CAT")); } }
DRIB TAC
Benchmark, string reversal. Suppose your program must reverse strings millions or billions of times. Can we use some simple logic to optimize string reversal? Here we test 2 methods.
Version 1 This method uses ToCharArray and Array.Reverse. It is short and simple, probably good enough for most programs.
Version 2 This version allocates an empty char array, and iterates backwards through the string and assigns elements in the array.
Result The iterative method ReverseStringDirect is faster—nearly twice as fast. It does not use Array.Reverse or ToCharArray.
using System; using System.Diagnostics; class Program { public static string ReverseString(string s) { char[] array = s.ToCharArray(); Array.Reverse(array); return new string(array); } public static string ReverseStringDirect(string s) { char[] array = new char[s.Length]; int forward = 0; for (int i = s.Length - 1; i >= 0; i--) { array[forward++] = s[i]; } return new string(array); } static void Main() { int sum = 0; const int _max = 10000000; Console.WriteLine(ReverseString("TEST")); Console.WriteLine(ReverseStringDirect("TEST")); // Version 1: reverse with ToCharArray. var s1 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { sum += ReverseString("PROGRAMMINGISFUN").Length; } s1.Stop(); // Version 2: reverse with iteration. var s2 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { sum += ReverseStringDirect("PROGRAMMINGISFUN").Length; } s2.Stop(); Console.WriteLine(s1.Elapsed.TotalMilliseconds); Console.WriteLine(s2.Elapsed.TotalMilliseconds); } }
TSET TSET 210.2514 ms ReverseString 392.5179 ms ReverseStringDirect
Performance notes. For optimal performance, use caching. Also consider just iterating through a string backwards instead of ever creating a string.
Summary. We saw a method that receives a string and returns the string with its characters in the reverse order. It is one of many examples of using char arrays for string manipulation.
Reverse Words
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Sep 17, 2024 (edit).
Home
Changes
© 2007-2025 Sam Allen