C Language: Pointers 16

Self-defined function using pointers(Selfcpy())

Important Question:

C program using pointers, write a user-defined function named “selfcpy()” to copy contents of one string onto another string?

example:

Given:
n1=”hello world”

copied string
n2=”hello world”

#include<stdio.h>
void selfcpy(char *p1,char *p2)
{
	while(*p1!='\0')
	{
		*p2=*p1;
		p1++;
		p2++;
	}
	*p2='\0';
}
int main()
{
	char n1[20],n2[20];
	printf("Enter source string ");
	gets(n1);
	selfcpy(n1,n2);
	printf("Source string %s\n",n1);
	printf("Copied string %s\n",n2);
	return(0);
}

Output:

Enter source string hello
Source string hello
Copied string hello


Enter source string Computer
Source string Computer
Copied string Computer