Search
 
SCRIPT & CODE EXAMPLE
 

CPP

C++ String Copy Example

#include <iostream>  
#include <cstring>  
using namespace std;  
int main()  
{  
    char key[25], buffer[25];  
    cout << "Enter the key string: ";  
    cin.getline(key, 25);  
    strcpy(buffer, key);  
    cout << "Key = "<< key << endl;  
    cout << "Buffer = "<< buffer<<endl;  
    return 0;  
}
Comment

string copy in cpp

//use '=' to copy string
string s1={"Hello"};
string s2;
s2=s1;	//now s2 will have a copu s1
Comment

c++ copy string

#include <iostream>
#include <string.h>

/*
std::copy(void *, void *, void *)
1st arg: source begin
2nd arg: source end
3rd arg: destination address
*/

int main() 
{
	char *mystring{new char[20]{}};
	
	strcat(mystring, "Its Grepper");
	
    char *deststring{new char[20]{}};
    
    strcat(deststring, "Hello ");
    
    std::copy(mystring + 4, mystring + 12, deststring + strlen(deststring));
    
    std::cout << deststring << std::endl;
    
    
    
    delete[] mystring;
    delete[] deststring;
  
  /*
  output : Hello Grepper
  */
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: hello world program in c++ 
Cpp :: how to split a string in c++ 
Cpp :: c++ call by reference 
Cpp :: lutris 
Cpp :: c++ split string by several space 
Cpp :: how to find the sum of a vector c++ 
Cpp :: how to return char* from function in c++ 
Cpp :: how to declare a 2d boolean vector in c++ 
Cpp :: how to code string to int converter c++ 
Cpp :: initialize vector of vector c++ 
Cpp :: how to sort in c++ 
Cpp :: c++ remove element from vector 
Cpp :: cout c++ 
Cpp :: change colour of output to terminal c++ 
Cpp :: c++ ternary operator 
Cpp :: Header for INT_MIN 
Cpp :: c++ vector of class objects 
Cpp :: fizzbuzz c++ 
Cpp :: c++ uint32_t 
Cpp :: cpp while 
Cpp :: c++ operator overloading 
Cpp :: how to use custom array in c++ 
Cpp :: cpp vector 
Cpp :: SUMOFPROD2 codechef solution 
Cpp :: char to string c++ 
Cpp :: c++ program to find lcm of two numbers 
Cpp :: run c++ program mac 
Cpp :: how to grab numbers from string in cpp 
Cpp :: operator precedence in cpp 
Cpp :: loop execution decending order in c 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =