Trying to copy a string to another string. As a basic learner I have tried maximum from my side to get the output but in the end of the program(point1) my logic is not working proper. Kindly refer my Input and output given below to get clear idea.
This Program copy a string.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n1,n2,loc;
char *p1, *p2;
printf("Enter size of p1\n");
scanf("%d", &n1);
p1 = (char*) malloc (n1 * sizeof(char));
printf("Enter the P1 String\n");
fflush(stdin);
gets(p1);
printf("\nEnter the size of p2\n");
scanf("%d", &n2);
p2 = (char*) malloc (n2 * sizeof(char));
printf("Enter the P2 String\n");
fflush(stdin);
gets(p2);
printf("\nEnter the Location to copy\n");
scanf("%d", &loc);
for(int i=loc-1;i<=n1;i++) //point 1
{
*(p1+i) = *(p1+i)+n2;
}
for(int i=0;i<=n2;i++)
{
*(p2+i) = *(p1+i)+loc;
}
printf("\n Final copy is\n");
printf("%d",p1);
free(p1);
free(p2);
return 0;
}
Expected:
Input:
google
microsoft
output:
Goomicrosoftgle
Actual:
Input:
google
microsoft
output:
[Some garbage values including given string]