Copying String


C Program to copy one string into another using a function.


#include  <stdio.h>
#include  <conio.h>

void  copy(char  str1[ ] , char  str2[ ]) ;

void main()
{
  char  str1[50] , str2[50] ;

  clrscr();

  printf("Enter source string: ") ;	 
  gets(str1) ;

  copy(str1,str2) ;

  printf("Copied string: %s" , str2) ;

  getch();
}

void  copy(char  str1[ ] , char  str2[ ])
{
  int  i , j ;

  for(i=0 , j=0 ; str1[i]!='\0' ; j++ , i++)
    str2[j]=str1[i] ;

  str2[j]='\0' ;   
}

Output :


Enter source string: This is MyString.
Copied string: This is MyString.