-
Notifications
You must be signed in to change notification settings - Fork 0
/
string-sum.c
66 lines (61 loc) · 1.51 KB
/
string-sum.c
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *strrev(char *string){
int l = strlen(string);
char *result = malloc(l);
int i = 0;
for(i=0; i<l; i++){
result[i] = string[l-i-1];
}
return result;
}
// char to int: 'n'-'0'
// int to char: n+'0'
char *sum_three_chars(char first, char second, char third) {
int result;
char *final = malloc(2);
result = (first - '0') + (second - '0') + (third - '0');
sprintf(final, "%02d", result);
return final;
}
char *sum_two_strings(char *first, char *second) {
int i = 0, fl = strlen(first), sl = strlen(second);
char carry = '0';
char *result;
char *tmp = malloc(3);
if(fl < sl) result = malloc(sl+2);
else result = malloc(fl+2);
while( (first[i] != '\0') && (second[i] != '\0') ) {
tmp = sum_three_chars(first[i], second[i], carry);
result[i] = tmp[strlen(tmp)-1];
carry = tmp[strlen(tmp)-2];
i++;
}
if(fl > sl){
while(first[i] != '\0'){
tmp = sum_three_chars(first[i], '0', carry);
result[i] = tmp[strlen(tmp)-1];
carry = tmp[strlen(tmp)-2];
i++;
}
if(carry!='0') result[i] = carry;
} else if(fl < sl){
while(second[i] != '\0'){
tmp = sum_three_chars(second[i], '0', carry);
result[i] = tmp[strlen(tmp)-1];
carry = tmp[strlen(tmp)-2];
i++;
}
if(carry!='0') result[i] = carry;
} else {
if(carry != '0')
result[i] = carry;
}
return result;
}
int main() {
char *f = "45649841516518615151845664236889417533";
char *s = "449849848494849841563348";
printf("%s\n", strrev(sum_two_strings(strrev(f), strrev(s))));
}