-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdd Strings.cpp
More file actions
45 lines (39 loc) · 1.21 KB
/
Add Strings.cpp
File metadata and controls
45 lines (39 loc) · 1.21 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public:
int makeEqualLength(string &num1, string &num2){
int len1 = num1.size();
int len2 = num2.size();
if(len1 > len2){
for(int i = 0; i< len1-len2;i++){
num2 = '0' + num2;
}
return len1;
}
else if(len1 < len2){
for(int i = 0; i< len2-len1;i++){
num1 = '0' + num1;
}
}
return len2;
}
string addStrings(string num1, string num2) {
string result = "";
int length = makeEqualLength(num1, num2);
int carry = 0;
cout<<num1<<" "<<num2<<endl;
for(int i = length - 1;i >=0; i--){
int first = num1[i] - '0';//int value of num1.at(i)
int second = num2[i] - '0';//int value of num2.at(i)
int sum = first + second + carry;
// cout<<"sum: "<<sum<<endl;
char ch=((sum%10)+'0');
result = ch+ result;
carry = sum/10;
}
if(carry){
char ch=(carry+'0');
result = ch + result;
}
return result;
}
};