-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathAddString.java
27 lines (25 loc) · 947 Bytes
/
AddString.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class AddString {
public static void main(String[] args) {
String number1 = "5";
String number2 = "10";
System.out.println(addStrings(number1, number2));
}
public static String addStrings(String num1, String num2) {
StringBuilder result = new StringBuilder();
int carry = 0, length = Math.max(num1.length(), num2.length());
for (int index = 0 ; index < length ; index++) {
int digit1 = getDigit(num1, index);
int digit2 = getDigit(num2, index);
int sum = digit1 + digit2 + carry;
result.append(sum % 10);
carry = sum / 10;
}
return result.append(carry > 0 ? carry : "").reverse().toString();
}
private static int getDigit(String number, int index) {
if (index < number.length()) {
return number.charAt(number.length() - index - 1) - '0';
}
return 0;
}
}