C Program to Copy String Using User Defined Function

This C program copies string using user defined function and without using string handling function strcpy().

C Source Code: String Copy User Defined Function


#include<stdio.h>

/* Function prototype */
void mystrcpy(char str2[30], char str1[30]);

/* Main function */
int main()
{
 char str1[30], str2[30];
 int i;

 printf("Enter string:\n");
 gets(str1);
 
 mystrcpy(str2, str1);
 
 printf("Copied string is: %s", str2);
 
 return 0;
}

/* Function Definition*/
void mystrcpy(char str2[30], char str1[30])
{
 int i;
 for(i=0;str1[i]!='\0';i++)
 {
  str2[i] = str1[i];
 }
 str2[i] = '\0';
}

Output

Enter string:
Welcome to C ↲
Copied string is: Welcome to C

Note: ↲ represents ENTER key is pressed.