Rahul Jha
Rahul Jha

Reputation: 1141

Find the bigger number out of two numbers stored as strings in C#

I am working with a very large input (trying to solve Next Palindrome problem in SPOJ). I need to compare large numbers and find out which is larger.All this without using BigInteger class other i could i have simply parsed BigInteger from the string and compared to get a result.

I tried string.compare(a,b) but the outputs produced suggest that it doesnt support sorting "numeric strings"

Upvotes: 1

Views: 162

Answers (1)

Dmytro Shevchenko
Dmytro Shevchenko

Reputation: 34591

  1. Find out which string is longer.
  2. Fill the beginning of the shorter string with zeroes to match the length of the first string:
shorterString = shorterString.PadLeft(longerString.Length, '0');
  1. Compare.

Update: I assumed that the numbers are integers. If they can be floating-point/decimal, then the algorithm will not be as simple.

Upvotes: 3

Related Questions