Table of contents
1.
Introduction
2.
Copying one String to Another String
2.1.
Using Standard Method
2.2.
Using Function
2.3.
Using Recursion
2.4.
Using String Library Function
3.
Frequently Asked Questions
3.1.
What is the difference between Strncpy and strcpy?
3.2.
Why is strcpy unsafe?
3.3.
What is the difference between strdup and strcpy in C?
3.4.
How do I copy a string to another variable?
3.5.
Does strcpy overwrite C?
4.
Conclusion
Last Updated: Mar 27, 2024

C Program to copy first string into second string

Author Amit Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we will learn how to copy one string into another string in the C programming language. We will try to do it with and without using library functions. We will also learn how to copy one string to another string using a pointer. Let’s see how many ways we can copy one string to another. 

A string is simply a collection of characters. The concluding character determines the string's value. 

Also Read, Binary to Hex Converter and C Static Function.

Copying one String to Another String

In C language, we have 4 ways to copy one string to another string. These ways are:

  • Using Standard Method
  • Using Function
  • Using Recursion
  • Using String Library Function

Let’s see all these methods in detail with their implementation. 

Using Standard Method

In this method, first we will read the string that is entered using gets() function and then store that string into s1.

Now, to copy one string to another string, we will follow these steps:

  1. Using the For loop, we will iterate with the structure “for(i=0; s[i]!=’\o’; i++)”. Copy the elements of string s1 into string s2 as s2[i]=s1[i] until string s1's last element is reached.
  2. Because every string ends with null, set the last element of the string s2 to null.

Now, we can check the results by printing both strings s1 and s2.

Let’s see the implementation:

 

#include <stdio.h> 
int main() { 
   char s1[1000];
   char s2[1000]; 
   printf("Enter any string: "); 
   gets(s1); 
   int i; 
   for(i=0;s1[i]!='\0';i++) { 
      s2[i]=s1[i]; 
   } 
   s2[i]='\0'; 
   
   printf("original string s1='%s'\n",s1); 
   printf("copied string   s2='%s'",s2); 
return 0; 
}
You can also try this code with Online C Compiler
Run Code

 

Output:

Using Function

We can create a function named stringCopy(). This function can be called by the main() function. We need to pass two strings as arguments.

stringCopy() function will copy the elements of one string to another string. Let’s see how:

  1. Using the For loop, we will iterate with the structure “for(i=0; s2[i]=s1[i]; i++)”. Copy the elements of string s1[i] into string s2[i] until the end of i is reached.
  2. As soon as the for loop is finished, we will initialize a null value in the end of string s2.

After that, the printing of both string s1 and s2 is done by the main() function.

Let’s see the implementation:

 

#include <stdio.h>
#include <string.h>
 
void stringCopy(char *s1,char *s2)
{
    for(int i=0;s2[i]=s1[i];i++);
    
    s2[i]='\0';
}
int main()
{
    char s1[1000],s2[1000]; 
    printf("Enter any string: ");
    gets(s1);
    stringCopy(s1,s2);
    
    printf("original string s1='%s'\n",s1);
    printf("copied string   s2='%s'",s2);
    return 0;   
}
You can also try this code with Online C Compiler
Run Code

 

Output:

You can also read about dynamic array in c and  Tribonacci Series.

Using Recursion

In this method, we will create a function named stringCopy() as well. This function can be called by the main() function. We need to pass two strings and an int as arguments.

Let’s see the step of the recursive function:

  1. Firstly, we will initialize a null value at s2[i] as soon as we get true for s1[i] = null.
  2. If the above condition is not true, we will copy the elements of s1[i] to s2[i].
  3. The stringCopy() function will call itself as stringCopy(s1, s2, ++i), until s1[i] = null.

After this, we will print all the elements of Strings s1 and s2.

Let’s see the implementation:

 

#include <stdio.h>
void stringCopy(char *str1, char *str2, int i)
{
    if(str1[i]=='\0')
    {
     str2[i]='\0';
     return;
}  
else
{
str2[i]=str1[i];
      stringCopy(str1, str2, ++i);
    }
}
int main() {
    char s1[1000];
    char s2[1000];  
 
    printf("Enter any string: ");
    gets(s1);
    int i; 
    stringCopy(s1,s2,0);
    printf("original string s1='%s'\n",s1);
    printf("copied string   s2='%s'",s2);
    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output:

Using String Library Function

There is a function in the String library named strcpy(String 1, String 2) that is used to copy the elements of one string to another.

The strcpy() function is present in the header file named string.h. 

 

Let’s see the implementation:

 

#include <stdio.h>
#include <string.h>
 
int main()
{
   char s1[1000],s2[1000];  
    int i;
    printf("Enter any string: ");
    gets(s1);
    strcpy(s2,s1);
    printf("original string s1='%s'\n",s1);
    printf("copied string   s2='%s'",s2);
    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output:


 

You can practice by yourself with the help of online c compiler.

Must Read what is storage class in c

Frequently Asked Questions

What is the difference between Strncpy and strcpy?

strcpy( ) function copies whole content of one string into another string. Whereas, strncpy( ) function copies portion of contents of one string into another string. If destination string length is less than source string, entire/specified source string value won't be copied into destination string in both cases.

Why is strcpy unsafe?

strcpy has no way of knowing how large the destination buffer is (i.e. there is no length parameter) so sloppy programming using it can lead to overrunning the buffer and corrupting other memory. Such an overrun can lead to crashes, odd behaviour and may be exploitable by malware authors.

What is the difference between strdup and strcpy in C?

The function strcpy() will not allocate the memory space to copy. A pointer to the string to copy and a pointer to place to copy it to should be given. The function strdup() will occupy / grab itself the memory space for copying the string to. This memory space needs to be freed up later when it is of no use anymore.

How do I copy a string to another variable?

Copying one string to another - strcpy.

strcpy can be used to copy one string to another. Remember that C strings are character arrays. You must pass character array, or pointer to character array to this function where string will be copied. The destination character array is the first parameter to strcpy .

Does strcpy overwrite C?

Once we call the strcpy() function, its content is overwritten with the new value of the source string. Hence, the strcpy() function does not append the content of the source string to the destination. Instead, it completely overwrites the destination string with the new value.

Conclusion

In this article, we have studied how to copy one string to another string. We discussed four ways to do that. We hope that this article has provided you with the help to enhance your knowledge regarding programming in C language and if you would like to learn more, check out our articles on jump statements in C/C++bitwise operators in C/C++ and C vs C++.

Check out this problem - Multiply Strings

Recommended Readings:


Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available, Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Merry Learning!

Live masterclass