To copy the contents of one string to another string using pointer

Thursday, December 4th, 2008

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(“\n\n\t ENTER A STRING…: “);
gets(str1);
stcpy(str1,str2);
printf(“\n\t THE COPIED STRING IS…: “);
puts(str2);
getch();
}
void stcpy(char *str1, char *str2)
{
int i, len = 0;
while(*(str1+len)!=’\0′)
len++;
for(i=0;i<len;i++)
*(str2+i) = *(str1+i);
*(str2+i) = ‘\0′;
}
Download exe and source code here.

Author Name :
Ranjith

Total : 12 Comments


12 Responses to “To copy the contents of one string to another string using pointer”

  1. saurabhchopra says:

    a beautiful program.

  2. Parveez says:

    wow nice program…….

  3. Mayuresh Sawant says:

    not working….u need to assign valid address to str2. check the updated code

    #include
    #include
    void stcpy(char *str1, char *str2);
    void main()
    {
    char ch; //—-> added

    char *str1, *str2=&ch; //—>assigned
    clrscr();
    printf(“\n\n\t ENTER A STRING…: “);
    gets(str1);
    stcpy(str1,str2);
    printf(“\n\t THE COPIED STRING IS…: “);
    puts(str2);
    getch();
    }
    void stcpy(char *str1, char *str2)
    {
    int i, len = 0;
    while(*(str1+len)!=’′)
    len++;
    for(i=0;i<len;i++)
    *(str2+i) = *(str1+i);
    *(str2+i) = ‘′;
    }

  4. AbhraJYOTi says:

    program by the website is fully correct,there is no need to assign any valid address for str2…

  5. Deepesh says:

    Hey thanx it came as lot of use to me :D

  6. vaishu says:

    Its nice. Easy to understand… Thank you…

  7. Sudhansu says:

    i lykd da program as it is short yet simple.

  8. sultana says:

    plz help me iam not getting the program of c i.e;sorting strings using pointers

  9. Prabhakar says:

    It helpd me a lot

  10. susan says:

    thanks…try 2 include more programs as i m a cs engineering student and my knowledge about c programming is nil!!!! :-) :-)

  11. susan says:

    thanks…try 2 include more programs as i m a cs engineering student and my knowledge about c programming is nil!!!! :-) :-)

  12. susan says:

    tks a lot..try 2 include more programs as i m a cs engineering student and my knowledge about c programming is nil!!!! :-) :-)

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: