C Language: Pointers 17

Self-defined function using pointers(Selfcat())

Important Question:

C program using pointers, write a user-defined function named “selfcat()” to add the contents of one string after the existing contents of the second string?

example:

Given:
n1=”hello”
n2=”World”

copied string
1. without space in between
n3=”helloworld”
2. with space in between
n3=”hello world”

#include<stdio.h>
#include<string.h>
void selfcat(char *p1,char *p2)
{
	int len,i;
	char *c;
	c=p2;
	len=strlen(p2);
	//printf("length = %d\n",len);
	for(i=0;i<len;i++)
	p2++;
	
	while(*p1!='\0')
	{
		*p2=*p1;
		p1++;
		p2++;
	}
	*p2='\0';
}
int main()
{
	char n1[20],n2[20];
	printf("Enter first string ");
	gets(n1);
	printf("Enter second string ");
	gets(n2);
	printf("First string %s\n",n1);
	printf("Second string %s\n",n2);
	selfcat(n1,n2);
	printf("Concatenated string %s\n",n2);
	return(0);
}

Output:

Enter first string hello
Enter second string world
First string hello
Second string world
Concatenated string worldhello