1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-11-08 18:49:07 +01:00
CodeWars/4kyu/sum_strings_as_numbers/solution.cs
Matej Focko fc899b0b02
chore: initial commit
Signed-off-by: Matej Focko <mfocko@redhat.com>
2021-12-28 16:19:58 +01:00

63 lines
1.3 KiB
C#

using System;
public static class Kata {
public static string sumStrings(string a, string b) {
var result = "";
var i = a.Length - 1;
var j = b.Length - 1;
var remainder = 0;
while (i >= 0 && j >= 0) {
var a_digit = a[i] - '0';
var b_digit = b[j] - '0';
var sum = a_digit + b_digit + remainder;
result = (sum % 10) + result;
if (sum >= 10) {
remainder = sum / 10;
} else {
remainder = 0;
}
i--;
j--;
}
while (i >= 0) {
var a_digit = a[i] - '0';
var sum = a_digit + remainder;
result = (sum % 10) + result;
if (sum >= 10) {
remainder = sum / 10;
} else {
remainder = 0;
}
i--;
}
while (j >= 0) {
var b_digit = b[j] - '0';
var sum = b_digit + remainder;
result = (sum % 10) + result;
if (sum >= 10) {
remainder = sum / 10;
} else {
remainder = 0;
}
j--;
}
if (remainder > 0) {
result = remainder + result;
}
return result.TrimStart('0');
}
}