#include <stdio.h>
#include <stdlib.h>
int ft_strlen(const char *src) {
int i = 0;
while (src[i]) i++;
return i;
}
char *ft_concat_params(int argc, char **argv) {
// Conta o tamanho das strings.
int count = 1; // 1 = espaço do terminador nulo.
for (int i = 0; i < argc; i++) {
count += ft_strlen(argv[i]);
}
// Cria a nova string que será retornada.
char *newString
= (char *) malloc(count
);
// Copia as strings para o newString.
// Usa aux para percorrer o espaço de memória de newString.
char *aux = newString;
for (int i = 0; i < argc; i++) {
for (int j = 0; argv[i][j]; j++, aux++) {
aux[0] = argv[i][j];
}
}
// Adiciona o terminador nulo.
aux[0] = 0;
// Terminamos.
return newString;
}
int main(void) {
const char *strings1[] = {"banana", "pera", "uva"};
char *a = ft_concat_params(3, strings1);
printf("[%s] - %d\n", a
, ft_strlen
(a
));
const char *strings2[] = {"", "teste", "", "mais", "", "perigoso", ""};
char *b = ft_concat_params(7, strings2);
printf("[%s] - %d\n", b
, ft_strlen
(b
));
const char *strings3[] = {"", "", ""};
char *c = ft_concat_params(3, strings3);
printf("[%s] - %d\n", c
, ft_strlen
(c
));
const char *strings4[] = {};
char *d = ft_concat_params(0, strings4);
printf("[%s] - %d\n", d
, ft_strlen
(d
));
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCmludCBmdF9zdHJsZW4oY29uc3QgY2hhciAqc3JjKSB7CiAgICBpbnQgaSA9IDA7CiAgICB3aGlsZSAoc3JjW2ldKSBpKys7CiAgICByZXR1cm4gaTsKfQoKY2hhciAqZnRfY29uY2F0X3BhcmFtcyhpbnQgYXJnYywgY2hhciAqKmFyZ3YpIHsKCiAgICAvLyBDb250YSBvIHRhbWFuaG8gZGFzIHN0cmluZ3MuCiAgICBpbnQgY291bnQgPSAxOyAvLyAxID0gZXNwYcOnbyBkbyB0ZXJtaW5hZG9yIG51bG8uCiAgICBmb3IgKGludCBpID0gMDsgaSA8IGFyZ2M7IGkrKykgewogICAgICAgIGNvdW50ICs9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