Related Articles

Related Articles

Different ways to copy a string in C/C++
  • Last Updated : 17 Dec, 2020

Using inbuilt function

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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.

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :