Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 03 Feb, 2023
Improve Article
Save Article

Using inbuilt function:

strcpy():

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 string
char* copyString(char s[])
{
    char* s2;
    s2 = (char*)malloc(20);
 
    strcpy(s2, s);
    return (char*)s2;
}
 
// Driver Code
int main()
{
    char s1[20] = "GeeksforGeeks";
    char* s2;
 
    // Function Call
    s2 = copyString(s1);
    printf("%s", s2);
    return 0;
}


Output

GeeksforGeeks

Using memcpy():

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 Code
int main()
{
    char s1[20] = "GeeksforGeeks";
    char s2[20];
 
    // Function
    memcpy(s2, s1, strlen(s1));
 
    printf("%s\n", s1);
 
    return 0;
}


Output

GeeksforGeeks

Using loops:

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 string
char* 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 Code
int main()
{
    char s1[20] = "GeeksforGeeks";
    char* s2;
 
    // Function Call
    s2 = copyString(s1);
    printf("%s", s2);
    return 0;
}


Output

GeeksforGeeks

Using pointers:

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 string
char* 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 Code
int main()
{
    char s1[20] = "GeeksforGeeks";
    char* s2;
 
    s2 = copyString(s1);
    printf("%s", s2);
    return 0;
}


Output

GeeksforGeeks

Using pointers and post-increment:

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 string
void 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 Code
int main()
{
    char s2[20] = "GeeksforGeeks";
    char s1[20];
 
    // Function Call
    copyString(s1, s2);
    printf("%s", s1);
    return 0;
}


Output

GeeksforGeeks

Using sprintf():

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 Code
int main()
{
    char s1[20] = "GeeksforGeeks";
    char s2[20];
 
    // Function
    sprintf(s2, "%s", s1);
 
    printf("%s\n", s1);
 
    return 0;
}


Output

GeeksforGeeks

Using strncpy():

Using the inbuilt function strncpy() from string.h header file to copy one string to the other. Function strcnpy() accepts a pointer to the destination array and source array as a parameter and the size of the destination array and after copying it returns a pointer to the destination string.

Syntax:

char * strncpy ( char * destination, const char * source, size_t num );

C




// C program to copy the string using
// strncpy function
 
#include <stdio.h>
#include <string.h>
 
// Function to copy the string
char* copyString(char s[])
{
    char* s2;
    s2 = (char*)malloc(20);
 
    strncpy(s2, s, 20);
    return (char*)s2;
}
 
// Driver Code
int main()
{
    char s1[20] = "GeeksforGeeks";
    char* s2;
 
    // Function Call
    s2 = copyString(s1);
    printf("%s", s2);
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


C++




// CPP program to copy the string using
// strncpy function
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to copy the string
char* copyString(char s[])
{
    char* s2;
    s2 = (char*)malloc(20);
 
    strncpy(s2, s, 20);
    return (char*)s2;
}
 
// Driver Code
int main()
{
    char s1[20] = "GeeksforGeeks";
    char* s2;
 
    // Function Call
    s2 = copyString(s1);
    cout << s2;
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


Output

GeeksforGeeks

Using strdup():

We use the inbuilt function strdup() from string.h header file to duplicate a string by allocating memory for a copy of the string using malloc, and then copying the string into the newly allocated memory. The function returns a pointer to the newly allocated copy of the string.

Syntax:

char *strdup(const char *s);

C




// C program to copy the string
// using strdup function
 
#include <stdio.h>
#include <string.h>
 
// Function to copy the string
char* copyString(char s[])
{
    char* s2;
    s2 = strdup(s);
    return (char*)s2;
}
 
// Driver Code
int main()
{
    char s1[20] = "GeeksforGeeks";
    char* s2;
 
    // Function Call
    s2 = copyString(s1);
    printf("%s", s2);
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


C++




// CPP program to copy the string
// using strdup function
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to copy the string
char* copyString(char s[])
{
    char* s2;
    s2 = strdup(s);
    return (char*)s2;
}
 
// Driver Code
int main()
{
    char s1[20] = "GeeksforGeeks";
    char* s2;
 
    // Function Call
    s2 = copyString(s1);
    cout << s2;
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


Output

GeeksforGeeks

Using strndup(): 

This function is similar to strdup(), but it can copies at most n bytes.

Syntax:

char *strndup(const char *s, size_t n); 

C




// C program to copy the string
// using strndup function
 
#include <stdio.h>
#include <string.h>
 
// Function to copy the string
char* copyString(char s[])
{
    char* s2;
    s2 = strndup(s, strlen(s));
    return (char*)s2;
}
 
// Driver Code
int main()
{
    char s1[20] = "GeeksforGeeks";
    char* s2;
 
    // Function Call
    s2 = copyString(s1);
    printf("%s", s2);
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


C++




// CPP program to copy the string
// using strndup function
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to copy the string
char* copyString(char s[])
{
    char* s2;
    s2 = strndup(s, strlen(s));
    return (char*)s2;
}
 
// Driver Code
int main()
{
    char s1[20] = "GeeksforGeeks";
    char* s2;
 
    // Function Call
    s2 = copyString(s1);
    cout << s2;
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


Output

GeeksforGeeks

Using std::string class (=) Operator:

The easiest way to copy a string is to use the assignment operator (=) to copy the contents of one string to another in std::string class.

Syntax:

std::string copy = original;

C++




// CPP program to copy the string
// using std::string function
 
#include <bits/stdc++.h>
 
// Function to copy the string
std::string copyString(std::string s)
{
    std::string s2 = s;
    return s2;
}
 
// Driver Code
int main()
{
    std::string s1 = "GeeksforGeeks";
    std::string s2;
 
    // Function Call
    s2 = copyString(s1);
    std::cout << s2;
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


Output

GeeksforGeeks

Using std::string::assign() Method:

We can use the std::string::assign() method to copy a string. This method takes a string as an argument and assigns its value to the string object on which the method is called.

Syntax:

string_object.assign(string_to_copy);

C++




// CPP program to copy the string
// using std::string::assign() method
 
#include <bits/stdc++.h>
 
// Function to copy the string
std::string copyString(std::string s)
{
    std::string s2;
 
    // std::string::assign() method
    s2.assign(s);
 
    return s2;
}
 
// Driver Code
int main()
{
    std::string s1 = "GeeksforGeeks";
    std::string s2;
 
    // Function Call
    s2 = copyString(s1);
    std::cout << s2;
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


Output

GeeksforGeeks

Using memmove() Function:

We can use memmove() function to copy a block of memory from a location to another. It is declared in string.h header file.

Syntax:

void * memmove(void *to, const void *from, size_t numBytes);

C




// C program to copy the string
// using memmove() function
 
#include <stdio.h>
#include <string.h>
 
int main(){
    char s1[20] = "GeeksforGeeks";
    char s2[20];
     
    // Copies contents of s1 to s2
    memmove(s2, s1, sizeof(s1));
     
    printf("%s",s2);
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


C++




// CPP program to copy the string
// using memmove() function
 
#include <bits/stdc++.h>
using namespace std;
 
int main(){
    char s1[20] = "GeeksforGeeks";
    char s2[20];
     
    // Copies contents of s1 to s2
    memmove(s2, s1, sizeof(s1));
     
    cout << s2;
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


Output

GeeksforGeeks

Using strcat() Function:

We can use strcat() function to append a copy of the source string in the destination string and a terminating Null character. It is declared in string.h (For C) and cstring(For C++) header file.

Syntax:

char *strcat(char *dest, const char *src);

C




// C program to copy the string
// using strcat() function
 
#include <stdio.h>
#include <string.h>
 
int main()
{
    char s1[20] = "GeeksforGeeks";
    char s2[20] = "";
 
    // Copies contents of s1 to s2
    strcat(s2, s1);
 
    printf("%s", s2);
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


C++




// CPP program to copy the string
// using strcat() function
 
#include <iostream>
#include <cstring>
using namespace std;
 
int main()
{
    char s1[20] = "GeeksforGeeks";
    char s2[20] = "";
 
    // Copies contents of s1 to s2
    strcat(s2, s1);
 
    cout << s2;
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


Output

GeeksforGeeks

Using strncat() Function:

We can use strncat() function to append not more than n characters from the string pointed to by src to the end of the string pointed to by dest plus a terminating Null-character. It is declared in string.h (For C) and cstring(For C++) header file.

Syntax:

char *strncat(char *dest, const char *src, size_t n)

C




// C program to copy the string
// using strncat() function
 
#include <stdio.h>
#include <string.h>
 
int main()
{
    char s1[20] = "GeeksforGeeks";
    char s2[20] = "";
 
    // Appends 13 characters from s1 to s2
    strncat(s2, s1, 13);
 
    printf("%s", s2);
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


C++




// CPP program to copy the string
// using strncat() function
 
#include <iostream>
#include <cstring>
using namespace std;
 
int main()
{
    char s1[20] = "GeeksforGeeks";
    char s2[20] = "";
 
    // Appends 13 characters from s1 to s2
    strncat(s2, s1, 13);
 
    cout << s2;
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


Output

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.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!