#include <stdio.h>
#include <string.h>
#include <stdlib.h> // Required for malloc
#define MAX 20
char *concatena(char *s1, char *s2);
int main() {
char s1[MAX], s2[MAX];
printf("Inserisci la prima stringa: ");
printf("Inserisci la seconda stringa: ");
char *tot = concatena(s1, s2);
printf("Stringa totale: %s\n", tot
);
free(tot
); // Free dynamically allocated memory return 0;
}
char *concatena(char *s1, char *s2) {
// Allocate enough memory for both strings + null terminator
char *concatenata
= malloc(l1
+l2
+1 *sizeof(char)); if (concatenata == NULL) {
printf("Errore di allocazione memoria!\n"); exit(1); // Exit if memory allocation fails }
// Copy the first string
for (int i = 0; i < l1; i++) {
concatenata[i] = s1[i];
}
// Append the second string
for (int i = 0; i < l2; i++) {
concatenata[l1 + i] = s2[i];
}
// Null-terminate the string
concatenata[l1 + l2] = '\0';
return concatenata; // Return the dynamically allocated string
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPiAvLyBSZXF1aXJlZCBmb3IgbWFsbG9jCgojZGVmaW5lIE1BWCAyMAoKY2hhciAqY29uY2F0ZW5hKGNoYXIgKnMxLCBjaGFyICpzMik7CgppbnQgbWFpbigpIHsKICAgIGNoYXIgczFbTUFYXSwgczJbTUFYXTsKCiAgICBwcmludGYoIkluc2VyaXNjaSBsYSBwcmltYSBzdHJpbmdhOiAiKTsKICAgIHNjYW5mKCIlcyIsIHMxKTsKCiAgICBwcmludGYoIkluc2VyaXNjaSBsYSBzZWNvbmRhIHN0cmluZ2E6ICIpOwogICAgc2NhbmYoIiVzIiwgczIpOwoKICAgIGNoYXIgKnRvdCA9IGNvbmNhdGVuYShzMSwgczIpOwoKICAgIHByaW50ZigiU3RyaW5nYSB0b3RhbGU6ICVzXG4iLCB0b3QpOwoKICAgIGZyZWUodG90KTsgLy8gRnJlZSBkeW5hbWljYWxseSBhbGxvY2F0ZWQgbWVtb3J5CiAgICByZXR1cm4gMDsKfQoKY2hhciAqY29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