C program to copy string without using strcpy() function

CServer Side ProgrammingProgramming

In this section we will see how to copy a string to other string without using strcpy() function. To solve this problem we can write our own function that can act like strcpy(), but here we will follow some trick. We will use another library function to copy a string into another.

The logic is very simple. Here we will use the sprintf() function. This function is used to print some value or line into a string, but not in the console. This is the only difference between printf() and sprintf(). Here the first argument is the string buffer. where we want to save our data.

Input − Take one string "Hello World"
Output − It will copy that string into another string. "Hello World"


Step 1: Take a string
Step 2: Create an empty string buffer to store result
Step 3: Use sprintf() to copy the string
Step 4: End

Example Code

 Live Demo

main() {
   char str[50]; //create an empty string to store another string
   char *myString = "Program to copy a String";
   sprintf(str, "%s", myString);//Use sprintf to copy string from myString to str
   printf("The String is: %s", str);


The String is: Program to copy a String
Published on 28-Mar-2019 07:56:36