How to reverse a string

Given a string and we need to write a method that takes this string and returns a reversed string.

The first what we need to do, it to understand what exactly is needed. Let's create several examples and based on the examples we'll create an algorithm.

Here are a couple of examples

  • if input string is abc the result will be cba
  • if input string is abcd the result will be dcba
  • if input string is abcdef the result will be fedcba

Let's take a look at abcdef in more details. To get the result we need to swap first char (a) and the last one (f). Then do the same for b and e and finally for c and d.

 

string

 

Okay, now we know the idea. To implement it we need to know what string is. In almost all programming languages a string is an array of chars.

The algorithm could be something like this:

  • Store pointer on a first char and store pointer on the last char
  • Swap first and last chars
  • Increment pointer on the first char and decrement the pointer on the last char

this algorithm has several issues:

  • it doesn't know when it should stop
  • doesn't cover corner cases

What does it mean the algorithm "doesn't know when it should stop". See, if we continue to increment the pointer on first char (move it to the right), it's just a matter of time when the pointer on first char will go outside of the input string and our algorithm will crash. The same is relevant for the pointer on the last char, it will go outside of input string during moving it to the left.

When to stop the algorithm

The answer to this question contains in the question. to reverse a string it means the algorithm should swap the left part of the string with right part of the string. In other words, the pointers should not cross the middle of the input string.

 

string

 

the last what we need to take care is:

  • empty string If an input string is empty, the algorithm should do nothing.

Algorithm

  • check an input string on empty
    • if empty, do nothing
  • store pointer on a first char and store pointer on the last char
    • while the pointer on a first char is less than a pointer on the last char, do:
      • swap first and last chars
      • increment pointer on the first char and decrement the pointer on the last char

 

string

Code

    public string ReverseString(string value)
    {
        // if input string is null or empty: do nothing
        if (string.IsNullOrEmpty(value))
        {
            return value;
        }

        // pointer on the first char
        int lo = 0;

        // pointer on the last char
        int hi = value.Length - 1;
        var builder = new StringBuilder(value);
        while (lo < hi)
        {
            // swap chars
            char dummy = builder[lo];
            builder[lo] = builder[hi];
            builder[hi] = dummy;

            // move the pointer to the right
            lo++;

            // move the pointer to the left
            hi--;
        }

        return builder.ToString();
    }