
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ String::copy() function
The C++ std::string::copy is used to copy a specified number of characters from a string object into a character array. It takes three parameters: a pointer to the destination character array, the number of characters to copy, and the starting position in the source string.
This function doesn't include a null character at the end of the copied content.
Syntax
Following is the syntax for std::string::copy() function.
size_t copy (char* s, size_t len, size_t pos = 0) const;
Parameters
- s − It indicates the pointer to an array of characters.
- len − It indicates the number of characters to copy.
- pos − It indicates the position of the first character to be copied.
Return value
It does not have any return value.
Exceptions
If an exception is thrown, there are no changes in the string.
Example 1
Following is an example to find the std::string::copy using C++.
#include <iostream> #include <string> int main() { char buffer[20]; std::string str("Sairamkrishna Mammahe..."); std::size_t length = str.copy(buffer, 6, 5); buffer[length] = '\0'; std::cout << "buffer contains: " << buffer << '\n'; return 0; }
Output
Let us compile and run the above program, this will produce the following result −
buffer contains: mkrish
Example 2
In the program we have initialized the stringX1, stringX2 and stringX5 by assigning the value and declared the stringX3 and stringX4 tells the existence of entity and its location.
#include <iostream> #include <cstring> using namespace std; int main() { char stringX1[] = " Good morning "; char stringX2[] = " Tutorialspont Company "; char stringX3[20]; char stringX4[20]; char stringX5[] = " TPC "; strcpy(stringX2, stringX1); strcpy(stringX3, " Copy successful "); strcpy(stringX4, stringX5); cout << "stringX1: " << stringX1 << "\nstrigX2: " << stringX2 << " \nstringX3: " << stringX3 << " \nstringX4: " << stringX4; return 0; }
Output
If we run the above code it will generate the following output.
stringX1: Good morning strigX2: Good morning stringX3: Copy successful stringX4: TPC
Example 3
In this program, the src string contains the value "Tutorialspoint" and we copy the source string to the target string using the copy() function.
#include <iostream> #include <string> using namespace std; int main() { string src = " Tutorialspoint"; char dest[15]; cout << " source string = " << src << '\n'; src.copy(dest, sizeof src); cout << " destination string = " << dest; return 0; }
Output
Following is the output of the above code.
source string = Tutorialspoint destination string = Tutorialspoint
Example 4
In the below program, we are passing the position as a parameter. So it copies the substring of string x to the string x1 using copy() function.
#include <iostream> #include <string> using namespace std; int main() { string x = "Tutorialspoint company"; char x1[22]; x.copy(x1, 12, 0); x1[10] = '\0'; cout << " String x1 = " << x1; return 0; }
Output
Following is the output of the above code.
String x1 = Tutorialsp