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.
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
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> int main() { char s1[1000],s2[1000]; int i; printf("Enter any string: "); gets(s1); for(i=0;s1[i]!='\0';i++) // or for(i=0;s1[i];i++) { s2[i]=s1[i]; } s2[i]='\0'; printf("original string s1='%s'\n",s1); printf("copied string s2='%s'",s2); return 0; } |
Output:
1 2 3 |
Enter any string: hello world original string s1='hello world' copied string s2='hello world' |
Using Function
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> #include <string.h> void stringcopy(char *s1,char *s2) { int i; for(i=0;s2[i]=s1[i];i++); s2[i]='\0'; } int main() { char s1[1000],s2[1000]; int i; printf("Enter any string: "); gets(s1); stringcopy(s1,s2); printf("original string s1='%s'\n",s1); printf("copied string s2='%s'",s2); return 0; } |
Output:
1 2 3 |
Enter any string: goutham original string s1='goutham' copied string s2='goutham' |
Using Recursion
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> void stringcopy(char *s1,char *s2,int i) { if(s1[i]=='\0') { s2[i]='\0'; return; } else { s2[i]=s1[i]; stringcopy(s1,s2,++i); } } int main() { char s1[1000],s2[1000]; int i; printf("Enter any string: "); gets(s1); stringcopy(s1,s2,0); printf("original string s1='%s'\n",s1); printf("copied string s2='%s'",s2); return 0; } |
Output:
1 2 3 |
Enter any string: cprogram original string s1='cprogram' copied string s2='cprogram' |
Using String Library Function
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#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; } |
Output:
1 2 3 |
Enter any string: hello c beginners original string s1='hello c beginners' copied string s2='hello c beginners' |