How to copy a string in C++

11 Answers

0 votes
// Copy using std::string assignment

#include <iostream>
#include <string>

int main() {
    std::string src = "Programming is fun";
    std::string dest = src;

    std::cout << dest << std::endl;
}



/*
run:

Programming is fun

*/

 



answered 4 days ago by avibootz
edited 4 days ago by avibootz
0 votes
// Copy using std::string constructor

#include <iostream>
#include <string>

int main() {
    std::string src = "Programming is fun";
    std::string dest(src);

    std::cout << dest << std::endl;
}



/*
run:

Programming is fun

*/

 



answered 4 days ago by avibootz
0 votes
// Copy using assign()

#include <iostream>
#include <string>

int main() {
    std::string src = "Programming is fun";
    std::string dest;

    dest.assign(src);

    std::cout << dest << std::endl;
}



/*
run:

Programming is fun

*/

 



answered 4 days ago by avibootz
0 votes
// Copy using strcpy (C-style)

#include <iostream>
#include <cstring>

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

    std::strcpy(dest, src);

    std::cout << dest << std::endl;
}



/*
run:

Programming is fun

*/

 



answered 4 days ago by avibootz
0 votes
// Copy using strncpy (C-style)

#include <iostream>
#include <cstring>

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

    std::strncpy(dest, src, sizeof(dest) - 1);
    dest[sizeof(dest) - 1] = '\0';

    std::cout << dest << std::endl;
}



/*
run:

Programming is fun

*/

 



answered 4 days ago by avibootz
0 votes
// Copy using memcpy

#include <iostream>
#include <cstring>

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

    std::memcpy(dest, src, std::strlen(src) + 1);

    std::cout << dest << std::endl;
}



/*
run:

Programming is fun

*/

 



answered 4 days ago by avibootz
0 votes
// Copy using manual loop

#include <iostream>

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

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

    std::cout << dest << std::endl;
}



/*
run:

Programming is fun

*/

 



answered 4 days ago by avibootz
0 votes
// Copy using pointer loop

#include <iostream>

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

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

    std::cout << dest << std::endl;
}


/*
run:

Programming is fun

*/

 



answered 4 days ago by avibootz
0 votes
// Copy using std::copy

#include <iostream>
#include <algorithm>
#include <cstring>

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

    std::copy(src, src + std::strlen(src) + 1, dest);

    std::cout << dest << std::endl;
}



/*
run:

Programming is fun

*/

 



answered 4 days ago by avibootz
0 votes
// Copy using snprintf

#include <iostream>
#include <cstdio>

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

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

    std::cout << dest << std::endl;
}


/*
run:

Programming is fun

*/

 



answered 4 days ago by avibootz
0 votes
// Copy using stringstream

#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string src = "Programming is fun";
    std::stringstream ss;

    ss << src;
    std::string dest = ss.str();

    std::cout << dest << std::endl;
}



/*
run:

Programming is fun

*/

 



answered 4 days ago by avibootz

Related questions

8 answers 266 views
6 answers 21 views
7 answers 20 views
8 answers 28 views
8 answers 25 views
7 answers 21 views
...