How to reverse integers with C# .NET
October 20, 2020 Leave a comment
Say you need to write a function that accepts and integer and returns its reverse. Here come a couple of examples:
Input | Output |
4 | 4 |
28 | 82 |
9876 | 6789 |
-4 | -4 |
-456 | -654 |
-1928 | -8291 |
So we’re not talking about some mathematical function, but more of a string-reverse. However, note that the negative sign must stay in place, so there’s a little bit of maths in there.
If that doesn’t sound like a problem that you would normally solve in a real life project then you’re probably right. I’m not even sure if there’s a legitimate business use-case for such a function. However, job interview tests often don’t revolve around real-life problems, at least not the initial ones that are meant to filter out candidates that are not a good fit for the position. It’s good to go through the most common problems so that you can breeze through them and have time over for the more difficult ones.
The C# solution is very easy in fact, and I’m sure it’s equally simple in other popular languages. Without any further due here’s one of the many possible solutions out there:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithms
{
public class IntReverse
{
public int ReverseInteger(int start)
{
var reverse = string.Join("", Math.Abs(start).ToString().Reverse());
return int.Parse(reverse) * Math.Sign(start);
}
}
}
Let’s see how this works.
Math.Abs(start).ToString()
The above bit takes the incoming integer, takes its absolute value and turns it to a string. Why take the absolute value? We don’t want the ‘-‘ character to end up at the end of the string, so at this point we get rid of it. So at this point we have the following strings:
Input | Output |
4 | “4” |
28 | “28” |
9876 | “9876” |
-4 | “4” |
-456 | “456” |
-1928 | “1928” |
Next we call the Reverse()
extension on this string. It returns an IEnumerable<char>
, i.e. not the reversed string, but the constituent characters in a reversed order like here:
Input | Output |
4 | [‘4’] |
28 | [‘8’, ‘2’] |
9876 | [‘6’, ‘7’, ‘8’, ‘9’] |
-4 | [‘4’] |
-456 | [‘6’, ‘5’, ‘4’] |
-1928 | [‘8’, ‘2’, ‘9’, ‘1’] |
The string.Join
function takes a delimiter and joins a collection into a string using that delimiter. Since the delimiter is an empty string it means that we don’t want anything in between the joined characters. This is the current state of things at this point:
Input | Output |
4 | “4” |
28 | “82” |
9876 | “6789” |
-4 | “4” |
-456 | “654” |
-1928 | “8291” |
The values in the Output column are assigned to the reverse
variable. It seems we have lost the negative sign on the way, but that’s OK, we can still read it from the incoming start
parameter. The Math
library has a function called Sign
which returns -1 for negative numbers, 0 for 0’s and +1 for positive numbers. So we parse the reverse
variable into an integer and multiply it with the correctly signed 1. Hence we have the reversed integer as specified and we can return it.
Here comes a set of unit tests:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Algorithms.Tests
{
[TestClass]
public class IntReverseTests
{
[TestMethod]
public void ReverseIntTests()
{
IntReverse intReverse = new IntReverse();
Assert.AreEqual(4, intReverse.ReverseInteger(4));
Assert.AreEqual(28, intReverse.ReverseInteger(82));
Assert.AreEqual(9876, intReverse.ReverseInteger(6789));
Assert.AreEqual(-4, intReverse.ReverseInteger(-4));
Assert.AreEqual(-456, intReverse.ReverseInteger(-654));
Assert.AreEqual(-1928, intReverse.ReverseInteger(-8291));
}
}
}
They pass as expected.
That’s it!