forked from pocketzeroes/proekt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-strings.c
More file actions
38 lines (37 loc) · 774 Bytes
/
Copy pathadd-strings.c
File metadata and controls
38 lines (37 loc) · 774 Bytes
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
void inplace_reverse(char * str)
{
if (str)
{
char * end = str + strlen(str) - 1;
# define XOR_SWAP(a,b) do\
{\
a ^= b;\
b ^= a;\
a ^= b;\
} while (0)
while (str < end)
{
XOR_SWAP(*str, *end);
str++;
end--;
}
# undef XOR_SWAP
}
}
char*addStrings(char*num1,char*num2){
char*result=strdup("");
char*ptr;
for (int i = strlen(num1) - 1, j = strlen(num2) - 1, carry = 0;
i >= 0 || j >= 0 || carry;
carry /= 10) {
if (i >= 0)
carry += num1[i--] - '0';
if (j >= 0)
carry += num2[j--] - '0';
asprintf(&ptr,"%s%d", result, carry % 10);
free(result);
result=ptr;
}
inplace_reverse(result);
return result;
}