Answers
#include "stdio.h"
int read_lint(char* ,int); // declarte the function to read line
void merge(char*,char*,char*); // declate the function to merge
int main(void) {
int n=1000; // set max characters n = 1000
char a1[n]; // declarate arrays a1,a2,a3;
char a2[n];
char a3[n+n];
char *str1=a1; // set pointers to arrays s1,s2,s3
char *str2=a2;
char *str3=a3;
merge(str3,str1,str2); // call the merge function.
printf("%s\n",str3); // print merged string
return 0;
}
void merge(char *str3,char *str1,char *str2){
int l1,l2,l3,min,i,j,n=1000; // declare variables
printf("Enter the first set of characters:");
l1 = read_line(str1,n); // get input string
printf("Enter the second set of characters:");
l2 = read_line(str2,n); // get input string
min = l1>l2?l2:l1; // calculate the min length of two strings
for(i=0,j=0;i<min;i++){ // until min length
*(str3+ j++) = *(str1+i); // add character to str3 from str1
*(str3+ j++) = *(str2+i); // add character to str3 from str2
}
while(l1>i){ // if str1 has more characters add it to str3
*(str3 + j++) = *(str1+ i++);
}
while(l2>i){ // if str2 has more characters add it to str3
*(str3 + j++) = *(str2+ i++);
}
}
int read_line(char *str, int n)
{
int ch, i = 0;
while ((ch = getchar()) != '\n')
{ if (i < n)
{ *str++= ch;
i++;
}
}
*str = '\0'; /* terminates string */
return i; /* number of characters stored */
}
/* sample output
Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl defend the castle
*/
.