How to reverse a string with C# .NET – Programming, Pseudocode Example, C# Programming Example
C# C# Console

How to reverse a string with C# .NET

This is a straightforward and basic task to implement: given a string input to a function we want to return its reverse.

There’s a wide range of solutions to this problem and here we’ll go through 3 of them using C#:

  • the built-in Reverse extension function
  • the Aggregate LINQ operator
  • bare-bones solution based on a for-loop

Here are the three implementations:

In ReverseStringWithReverse we take the incoming string and apply the built-in Reverse() extension method on it. It returns a collection of chars, i.e. not the reversed string. To join those chars into a string we can call string.Join or string.ConcatJoin takes a delimiter which is an empty string in this case as we don’t want to put any extra character in between the characters when they are joined. The Concat function is more straightforward to use, we can just pass in the array collection.

In ReverseStringWithLoop we iterate through the input string starting at the back. We build the reversed string character by character and return the result.

ReverseStringWithAggregate is a bit more “exotic” and uses the Aggregate LINQ operator. I only put it here in case you’d like to show your familiarity with this accumulator function in an interview. The empty string is a seed which is basically the starting point for building the reversed string.

This overload of the Aggregate function will operate on an IEnumerable<char> as the source string is dissected into its characters. We specified the seed as a string type so the overall result from this extension function will also be a string.

We start with an empty string and apply a function on the source string defined by

This is the accumulator function and accepts two parameters: the first one, i.e. c1 will be of the same type as the seed, i.e. a string. The second one, i.e. c2 will be the same type as each element in the dissected string, i.e. a char. The accumulator function returns the same type as the seed, i.e. a string which is simply built up gradually as

…i.e. take the character and add it to the growing string c1.

Let’s try to understand this function using the input string “hello”.

So we start with an empty string as c1 and take the first character from the source char collection, i.e. ‘h’. We get "" + 'h' = "h" as the resulting string which will be fed as the c1 string input into the next iteration. So next c1 will be “h” and c2 ‘e’ and we return ‘e’ + “h”, i.e. “eh”. “eh” is taken as c1 in the next round and ‘l’ as the character input, ‘l’ + “eh” will give “leh”. The function continues until we arrive at “olleh”. If we set the seed to “world” then that will be value of c1 in the very first iteration and the end result will be “ollehworld”.

It is probably overkill to use the Aggregate extension for a problem like this but it’s a good opportunity to demonstrate your familiarity with this relatively rarely used function in an interview setting.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: