# I believe this is correct answer, but why does it fail in a large test case?

• char* addBinary(char* a, char* b) {
char ta = *a;
char tb = b;
int cta = 0;
int ctb = 0;
int minc = 0;
int maxc = 0;
int savebig;
char
result = malloc(sizeof(char)*100);

``````while(ta != NULL){
a++;
cta++;  //count the length of char a
ta = *a;
}
// ta = *(--a);
while(tb != NULL){
b++;
ctb++;  //count the length of char b
tb = *b;
}
// tb = *(--b);

char upbit = '0';
// printf("a len: %d, b len: %d \n", cta, ctb);
char* bigone;
char* small;
if(ctb < cta){
bigone = malloc(sizeof(char)*(cta+1));
small = malloc(sizeof(char)*(ctb+1));
bigone = a;
small = b;
minc = ctb;
maxc = cta;
savebig = maxc;
}else{
bigone = malloc(sizeof(char)*(ctb+1));
small = malloc(sizeof(char)*(cta+1));
bigone = b;
small = a;
minc = cta;
maxc = ctb;
savebig = maxc;
}
tb = *(--bigone);
ta = *(--small);

// minc = cta;
while(minc>0){
if(tb=='1' && tb-ta==0){
if(upbit == '1'){
result[maxc] = '1';

}else{
result[maxc] = '0';
upbit = '1';
}
}else if(tb=='0' && tb-ta==0){
if(upbit == '1'){
result[maxc] = '1';
upbit = '0';
}else{
result[maxc] = '0';

}
}else{
if(upbit == '1'){
result[maxc] = '0';
}else{
result[maxc] = '1';
}
}

maxc--;
minc--;
ta = *(--small);
tb = *(--bigone);
}

while(maxc>0){
// printf("%c ;", tb);
if(upbit == '1' && tb == '1'){
result[maxc] = '0';
}else if(upbit == '1' && tb == '0'){
result[maxc] = '1';
upbit = '0';
}else if(upbit == '0' && tb == '1'){
result[maxc] = '1';
}else if(upbit == '0' && tb == '0'){
result[maxc] = '0';
}
maxc--;
tb = *(--bigone);
}

if(upbit == '1'){
result[maxc] = '1';
return result;
}else{
char* newres = malloc(sizeof(char)*(savebig+1));
for(int n=savebig; n>0; n--){
newres[n-1] = result[n];
}
// result[maxc] = '0';
return newres;
}

return result;
``````

}

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.