Q:

Write C++ program to copy one string to another string

belongs to collection: C++ language Pointer Exercises

0

Write C++ program to copy one string to another string

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

#include <iostream>
#define MAX_SIZE 100 // Maximum size of the string
using namespace std;
 
int main() {
 
    char text1[MAX_SIZE], text2[MAX_SIZE];
    char * str1 = text1;
    char * str2 = text2;
 
    // Inputting string from user
    cout<<"Enter any string: "<<endl;
    cin>>text1;
 
    // Coping text1 to text2 character by character
    while(*(str2++) = *(str1++));
 
    cout<<"First string: "<<text1<<endl;;
    cout<<"Second string: "<<text2<<endl;
 
    return 0;
}

Result:

Enter any string: 

Techstudy

First string: Techstudy

Second string: Techstudy

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write C++ program to concatenate two strings using... >>
<< Write C++ program to find length of string using p...