Program to Copy String in C Language

Program-to-Copy-String-in-C-Language

Program Description:

Write a Program to Copy String in C programming language. The program should accept a string (character array) from the user and copy it to new string.

Example Input and Output:

Input:

Enter a string : sillycodes.com

Output:

Original String: sillycodes.com
Copied String : sillycodes.com

Prerequisites:

Please go through the following articles to better understand this program.

We are going to look at three different methods to copy a string in C programming

  1. Iterative Method: Manually Copying the elements of the original string to a new string.
  2. User-Defined Function: Using a user-defined function(without strcpy) to copy the string.
  3. strcpy library function: Using the strcpy library function.

Copy String in C without using strcpy function (Iterative Method):

We can copy a string to a new string by traversing the original string element by element and copying each element to the new string.

Here is the algorithm of the manual string copy program.

Program Algorithm:

  1. Create Two strings named origStr and dupStr with size of SIZE ( i.e SIZE = 100). The dupStr should have enough size to hold the origStr.
  2. Prompt the user to provide the original string( origStr) and update the origStr variable.
  3. To Copy the original string to a new string, Iterate over the Original String using a For Loop and Copy elements from origStr to dupStr
    • FOR Loop – Start with i=0 and continue till origStr[i] != '\0'.
    • At each iteration copy origStr[i] to dupStr[i] – i.e dupStr[i] = origStr[i];
  4. Finally, add the terminating NULL character to dupStr string. – dupStr[i] = '\0';
  5. Display the results on the console.

📢 This Program is part of the C String Practice Programs series

Program to Copy String to a new string:

Program Output:

Compile and Run the program ( We are using GCC compiler on Ubuntu Linux OS)

Let’s try a few more examples.

string-copy-program-iterative-method

The program is properly copying the original string to the new string.

Program to Copy string using a user-defined function in C:

Let’s look at the second method to copy a string to a new string. In this method, We are going to create a user-defined function to copy the string.

In the following program, We have defined a user-defined function called mystrcpy

char * mystrcpy(char *dest, char *src)

The mystrcpy() function takes two character pointers as the formal arguments. The first argument is dest and second argument is src. The mystrcpy() function copies the characters from the src string to the dest string. This function also returns a character pointer pointing to the dest string (i.e resultant string).

Duplicate string using custom strcpy function C Program:

Here is the program to duplicate a string without using the strcpy function.

The mystrcpy() function also uses a for loop to iterate over the src string and copies the elements to dest string.

Program Output:

Let’s compile and run the program.

string-copy-using-custom-strcpy-function

In the first example, User provided the VS Code as the original string( origStr) and mystrcpy() function copied it to the new string ( dupStr).

Copy string using the strcpy function in C Language:

The C Standard library also provides strcpy library function to copy a string to new string. So let’s use this function to create a duplicate string.

The strcpy() function is available as part of the string.h header file. So we need to include the string.h header file to use the strcpy() function.

📢We have covered the strcpy() library function in detail in the following article

Program to Copy String using strcpy function:

Here is the program to copy a string using the strcpy library function

Program Output:

Compile and run the program and observe the output.

duplicate-string-using-strcpy-function

As expected program is properly copying the source string to destination string using strcpy function.

📢It is recommended to use the strcpy() Library function to copy strings instead of using the custom implementations. As library functions are fast and more efficient.

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Copy a String to New String […]

Leave a Reply