Re: Very intuitive solution accepted as best in C
Your solution is very simple and straight forward and I have learnt a lot about how to add two strings and deal with the carry problem. However, I found their are two bugs in your code which may cause some serious problems.
- in function
addString， when we add two numbers, the sum's length would be no greater than the length of the longer one plus 1. Additionally,
t, a char array that denotes the sum must end with
'\0', so the sum's length should be plus 1 again to guarantee
twould end correctly. Finally, when using
malloc, the line should be
char *t = (char*)malloc(sizeof(char)*((len1 > len2 ? len1 : len2) + 2)).
- memory has to be
freeed after being used. So making
ta global variable and freeing it before
isAdditiveNumbershould fix this.