So I got this assignment to combine two arrays without using strcat(). The two arrays must be passed to function as parameters. The arrays need to be merged into one array in the function with dynamic memory allocation and then the new string needs to be returned back to main.
Here's what I have coded so far, but it still prints nothing or gives warning:
d21.c:18: warning: format '%s' expects type 'char *', but argument 2 has type 'int'
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char combinearrays(const char *array1, const char *array2);
int main () {
int i;
const char *array1[50] = {"asgfhararjsrj"};
const char *array2[50] = {"wajwarjwrjarjsdafj"};
char cjj[100];
cjj[100]=combinearrays(*array1, *array2);
for (i=0; i!='\0'; i++){
printf("%s", cjj[i]);
}
putchar('\n');
return 0;
}
char combinearrays(const char *array1, const char *array2) {
int i;
int j=0;
char *text;
text = malloc(( strlen( array1 ) + strlen( array2 ) + 2 )* sizeof(char));
if (!text) {
printf ("Not enough memory");
exit(1);
}
for (i=0; i < (strlen( array1 )); i++) {
text[i] = array1[i];
}
for (j=strlen(array1); j < (strlen( array1 ) + strlen( array2 ) ); j++) {
text[j] = array2[j];
}
text[j+1]='\0';
return *text;
free (text);
}
This just seems to be impossible for me to solve alone.