Latest :

C Program To Copy One String To Another String | 4 Simple Ways

C program to copy one string to another string – In this article, we will discuss the available methods to copy one string to another string in C programming.

Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.

The methods used in this blog are as follows:

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

A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.

C Program To Copy One String To Another String

As you can see in the image uploaded above, firstly, you need to enter any random string of your choice.

The string entered here is as follows:

‘cprogram’

Thus, if this string is copied to another empty string, the second string will be like this:

‘cprogram’

Since the second string is empty, the first string is completely copied and the second one becomes a replica of the first.

Thus, the means to do the same in C programming are as follows:

Using Standard Method

  1. Read the entered string using gets() function and store the string into s1.

2) To copy one string into another string

a) Iterate the for loop with the structure for(i=0;s[i]!=’\0′;i++)

copy the elements of the string s1  into the string s2 as s2[i]=s1[i] until the last element of string s1.

b) Every string ends with null so initialize the last element as null in the string s2.

3) Print the original string s1 and copied string s2.

Output:

Using Function

  1. The main() function calls the stringcopy() function by passing s1,s2 as arguments.

2) The function stringcopy() will copy the one string elements into the another string.

a) for loop iterates with the structure for(i=0;s2[i]=s1[i];i++),The element of the string s1[i] will be copied to s2[i] until all iterations of i.

b) After completion of for loop initialize the null value as ending character to the string s2.

3) Then the main() prints the orriginal string s1 and copied string s2.

Output:

Using Recursion

  1. The main() function calls the recursion function stringcopy() by passing s1,s2,0 as arguments.

2) The recursion function

a) Initialize the null value to s2[i] if the element of s1[i]=null

b) Else the element s1[i] will be copied to s2[i].

c) The function calls itself as stringcopy(s1,s2,++i) until the element of the string s1 is null.

3) The main() function will print the elements of string s1 and copied string s2.

Output:

Using String Library Function
  1. The strcpy(string1, string2) is the one of the string library function to copy one string to another string.

2) The strcpy() function is available in string.h header file.

Output:

x

Check Also

C Program To Find Reverse Of An Array – C Programs

C program to find the reverse of an array – In this article, we will ...