Google News
logo
C++ Program to Copy Strings
In the following example of C++ program to copy strings :
Program :
#include 
#include 
using namespace std;

int main() {
   char str1[100], str2[100];
   cout << "Enter a string: ";
   cin.getline(str1, 100);
   strcpy(str2, str1);
   cout << "The copied string is: " << str2 << endl;
   return 0;
}
Output :
Enter a string: www.freetimelearning.com
The copied string is: www.freetimelearning.com
* In this program, we first declare two character arrays str1 and str2 of size 100.

* We then prompt the user to enter a string and read the input using the getline() function.

* Next, we use the strcpy() function to copy the contents of str1 to str2.

* Finally, we display the copied string str2 using cout.