This is the simple implementation of the “Copy” function using the pointers. This program copies the content of one string to another.
Logic : The program asks the user to input the string to copy and stores using the pointer str1. The inner function “stcpy” takes 2 string pointers as arguments. By keeping the length as the reference, it traces till EOF , for each i of this iteration it copies the i-th letter to the destination string, i.e str2. After the EOL, it puts a NULL to the second line. That gives the duplicated string of the entered.
This function copies the string using the pointer, where earlier program copies the string directly.
#include<stdio.h>
#include<conio.h>
void stcpy(char *str1, char *str2);
void main()
{
char *str1, *str2;
clrscr();
printf(“nnt ENTER A STRING…: “);
gets(str1);
stcpy(str1,str2);
printf(“nt THE COPIED STRING IS…: “);
puts(str2);
getch();
}
void stcpy(char *str1, char *str2)
{
int i, len = 0;
while(*(str1+len)!=’