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:
- 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.
- 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:
You can also try this code with Online C Compiler |
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:
- 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.
- 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:
You can also try this code with Online C Compiler |
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:
- Firstly, we will initialize a null value at s2[i] as soon as we get true for s1[i] = null.
- If the above condition is not true, we will copy the elements of s1[i] to s2[i].
- 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:
You can also try this code with Online C Compiler |
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:
You can also try this code with Online C Compiler |
Output:
You can practice by yourself with the help of online c compiler.
Must Read what is storage class in c