Different ways to copy a string in C/C++

06 Jun, 2021
Using the inbuilt function strcpy() from string.h header file to copy one string to the other. strcpy() accepts a pointer to the destination array and source array as a parameter and after copying it returns a pointer to the destination string. Using %s we can print the string(%s prints the string from the base address till the null character).
Below is the implementation using the above method:
C
// C program to copy the string using// strcpy function #include <stdio.h>#include <stdlib.h>#include <string.h> // Function to copy the stringchar* copyString(char s[]){    char* s2;    s2 = (char*)malloc(20);     strcpy(s2, s);    return (char*)s2;} // Driver Codeint main(){    char s1[20] = "GeeksforGeeks";    char* s2;     // Function Call    s2 = copyString(s1);    printf("%s", s2);    return 0;}
GeeksforGeeks
 
memcpy() is also defined in string.h header and used to copy from source to destination no matter what the source data contains. memcpy() requires a size parameter be passed.
The main difference is that memcpy() always copies the exact number of specified bytes ; strcpy() and other str methods, on the other hand, will copy until it reads a NULL (‘\0’) byte, and then stop after that. strcpy() is not intended to be used with zero-terminated C-strings.
memcpy() is hardware optimized and copy faster and work with any type of source data (like: binary or encrypted bytes). strcpy() should never use unless any specific reason, and f you know the lengths of the strings, memcpy() is a better choice.
C
// C program to copy the string using// memcpy function#include <stdio.h>#include <string.h> // Driver Codeint main(){    char s1[20] = "GeeksforGeeks";    char s2[20];     // Function    memcpy(s2, s1, strlen(s1));     printf("%s\n", s1);     return 0;}
GeeksforGeeks
The idea is to use a for loop to copy the content of the first array to the second array one by one.
Below is the implementation using the above method:
C
// C program to copy string using loops #include <stdio.h>#include <stdlib.h>#include <string.h> // Function to copy the stringchar* copyString(char s[]){    int i;    char* s2;    s2 = (char*)malloc(20);     // Executing till null character    // is found    for (i = 0; s[i] != '\0'; i++) {         // Copy the character one        // by one from s1 to s2        s2[i] = s[i];    }     // Return the pointer of newly    // created string    return (char*)s2;} // Driver Codeint main(){    char s1[20] = "GeeksforGeeks";    char* s2;     // Function Call    s2 = copyString(s1);    printf("%s", s2);    return 0;}
GeeksforGeeks
 
The idea is to copy the contents of the string array to another array using pointers and print the resultant string by traversing the new pointer.
Below is the implementation using the above method:
C
// C program to copy the string// using pointers #include <stdio.h>#include <stdlib.h>#include <string.h> // Function to copy the stringchar* copyString(char s[]){     char *s2, *p1, *p2;     s2 = (char*)malloc(20);    p1 = s;    p2 = s2;     // Executing till the null    // character is found    while (*p1 != '\0') {         // Copy the content of s1 to s2        *p2 = *p1;        p1++;        p2++;    }    *p2 = '\0';     return s2;} // Driver Codeint main(){    char s1[20] = "GeeksforGeeks";    char* s2;     s2 = copyString(s1);    printf("%s", s2);    return 0;}
GeeksforGeeks
 
The idea is to use a while loop to assigns the content of string array1 to string array2 one by one and increment using post operator as it returns ASCII value so the condition will be true and it will be in the loop and transfer till the condition is false, and we will come out of the loop.
Below is the implementation using the above method:
C
// C program to copy the string #include <stdio.h>#include <stdlib.h> // Function to copy the stringvoid copyString(char* t, char* s){    // (return ASCII value which is True,    // therefore will be in the loop    // till the condition is False    while (*t++ = *s++)        ;} // Driver Codeint main(){    char s2[20] = "GeeksforGeeks";    char s1[20];     // Function Call    copyString(s1, s2);    printf("%s", s1);    return 0;}
GeeksforGeeks
 
Instead of print the string in output buffer we can store it in specified char buffer or destination string in sprintf() to copy the string. 
C
// C program to copy the string using// sprintf function#include <stdio.h> // Driver Codeint main(){    char s1[20] = "GeeksforGeeks";    char s2[20];     // Function    sprintf(s2, "%s", s1);     printf("%s\n", s1);     return 0;}
GeeksforGeeks
 
Note: In all the above methods the size of the destination array must be greater than the length of the source string to copy all the characters.
 
scorchingeagle
C-String
cpp-strings-library
Technical Scripter 2020
C++
Program Output
Strings
Technical Scripter
Strings
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.