In: Computer Science
Can you add code to this so that it finishes the combine function. Given two words as input, the output of the program should be the two words combined together with a space between them. Not allowed to use any external libraries for this assignment.
#include <stdio.h>
#include <stdlib.h>
void combine(char* p, char* q){
/* Add the necesary logic here to combine the strings
in the dynamic array p and q by inserting a space
between two words
and write the result back to p */
}
int main(){
/* No changes should be done in this part */
char* word1 = malloc(sizeof(char) * 128);
char* word2 = malloc(sizeof(char) * 128);
printf("Enter your first word:\t");
scanf("%s", word1);
printf("Enter your second word:\t");
scanf("%s", word2);
combine(word1, word2);
printf("%s\n", word1);
}
Program :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void combine(char* p, char* q)
{
int l1;
int l2;
int i;
l1=strlen(p);
l2=strlen(q);
p[l1]=' ';
while(i<l2)
{
p[l1+1]=q[i];
l1=l1+1;
i=i+1;
}
}
int main()
{
/* No changes should be done in this part */
char* word1 = malloc(sizeof(char) * 128);
char* word2 = malloc(sizeof(char) * 128);
printf("Enter your first word:\t");
scanf("%s", word1);
printf("Enter your second word:\t");
scanf("%s", word2);
combine(word1, word2);
printf("%s\n", word1);
return 0;
}
Screenshot of the program:
Output of program :