How to copy a string in C

8 Answers

0 votes
#include <stdio.h>
#include <string.h>

// Copy using strcpy

#define LEN 64  
 
int main(void)
{
    char s1[LEN] = "";
    char s2[] = "Programming is fun";
    char s3[LEN] = "";
    char s4[LEN] = "";
 
    // Copy s2 into s1
    strcpy(s1, s2);
    printf("s1 = %s | s2 = %s\n", s1, s2);
 
    // Copy a literal string into s3
    strcpy(s3, "Write code in C is fun");
    printf("s3 = %s\n", s3);
 
    // Demonstrate strcpy return value
    printf("s4 = %s\n", strcpy(s4, "strcpy returns the destination buffer"));
 
    return 0;
}
 
 
/*
run:
 
s1 = Programming is fun | s2 = Programming is fun
s3 = Write code in C is fun
s4 = strcpy returns the destination buffer

*/


answered Jul 12, 2014 by avibootz
edited Apr 16 by avibootz
0 votes
#include <stdio.h>
#include <string.h> // strdup
#include <stdlib.h> // free

// Copy using strdup

int main() {
    const char *s = "Programming is fun";
    char *copy = strdup(s);

    if (copy == NULL) {
        perror("strdup failed");
        return 1;
    }

    printf("s: %s\n", s);
    printf("copy: %s\n", copy);

    free(copy);  // Always free what strdup allocates
    
    return 0;
}


/*
run:
 
s: Programming is fun
copy: Programming is fun

*/

 



answered Apr 16 by avibootz
edited Apr 16 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

// Copy using memcpy

int main() {
    const char *src = "Programming is fun";
    char dest[64] = "";

    // Copy the string (including null terminator)
    memcpy(dest, src, strlen(src) + 1);

    printf("Copied string: %s\n", dest);
    
    return 0;
}



/*
run:
 
Copied string: Programming is fun

*/

 



answered Apr 16 by avibootz
edited Apr 16 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

// Copy using strncpy

// strcpy = No bounds checking: strcpy will overflow the buffer if dest is too small.
// This is you can use strncpy or strlcpy (where available).

int main() {
    const char *src = "Programming is fun";
    char dest[64];  

    strncpy(dest, src, sizeof(dest) - 1);
    dest[sizeof(dest) - 1] = '\0';  // Ensure null termination


    printf("Source: %s\n", src);
    printf("Copy:   %s\n", dest);

    return 0;
}


/*
run:
 
Source: Programming is fun
Copy:   Programming is fun

*/

 



answered Apr 16 by avibootz
edited Apr 16 by avibootz
0 votes
#include <stdio.h>

// Copy using pointer‑based loop

int main() {
    const char *src = "Programming is fun";
    char dest[50];
    char *d = dest;

    while ((*d++ = *src++) != '\0');

    printf("%s\n", dest);
    
    return 0;
}



/*
run:
 
Programming is fun

*/

 



answered Apr 16 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// Copy using malloc + memcpy

int main() {
    const char *src = "Programming is fun";
    size_t len = strlen(src) + 1;

    char *dest = malloc(len);
    memcpy(dest, src, len);

    printf("%s\n", dest);
    free(dest);
    
    return 0;
}



/*
run:
 
Programming is fun

*/

 



answered Apr 16 by avibootz
0 votes
#include <stdio.h>

// Copy using snprintf

int main() {
    const char *src = "Programming is fun";
    char dest[50];

    snprintf(dest, sizeof(dest), "%s", src);

    printf("%s\n", dest);
    
    return 0;
}



/*
run:
 
Programming is fun

*/

 



answered Apr 16 by avibootz
0 votes
#include <stdio.h>

// Copy using a manual loop

int main() {
    const char *src = "Programming is fun";
    char dest[64] = "";
    int i = 0;

    while ((dest[i] = src[i]) != '\0') {
        i++;
    }

    printf("%s\n", dest);
    
    return 0;
}



/*
run:
 
Programming is fun

*/

 



answered Apr 16 by avibootz

Related questions

11 answers 89 views
6 answers 92 views
7 answers 98 views
8 answers 108 views
8 answers 108 views
7 answers 98 views
...