Swapping String in C++


Example Program:

Here is the simple program for printing swapping the given two string in C++.

Source Code

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str1 = "TutorJoe's Educational Centre";
    string str2 = "TutorJoe's Students";
    char character[80];
    str1.copy(character,10,0);
    cout <<"\nThe new copied character array is : ";
    cout <<character<<endl<<endl;
    cout <<"\nThe 1st string before swapping is : ";
    cout <<str1<<endl;
    cout <<"\nThe 2nd string before swapping is : ";
    cout <<str2<<endl;
    str1.swap(str2);
    cout <<"\nThe 1st string after swapping is : ";
    cout <<str1<<endl;
    cout <<"\nThe 2nd string after swapping is : ";
    cout <<str2<<endl;
    return 0;
}
To download raw file Click Here

Output

The new copied character array is : TutorJoe's

The 1st string before swapping is : TutorJoe's Educational Centre

The 2nd string before swapping is : TutorJoe's Students

The 1st string after swapping is : TutorJoe's Students
The 2nd string after swapping is : TutorJoe's Educational Centre


Basic Programs


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples



Learn All in Tamil © Designed & Developed By Tutor Joes