Answer:
The following are the code
string s1="san";
string s2="ran";
string temp; // other variable
temp=s1;// statement 1
s1=s2;// statement 2
s2=temp;//statement 3
Step-by-step explanation:
In this code we declared two string s1 , s2 and initialized them after that we declared an extra variable temp .
The statement 1 temp=s1 means that temp will store the value of s1 i.e "san". so temp="san";
The second statement 2 s1=s2 means that s1 will store the value of s2
i.e s1="ran";
The third statement 3 s2=temp means that s2 will store the value of temp.
i.e s2="san"
we see that s1 and s2 will exchange their value
Following are the code in c++
#include <iostream> // header file
#include<string>
using namespace std;
int main()// main function
{
string s1="san";
string s2="ran";
string temp;
cout<<"before exchange:"<<s1<<s2<<endl; // display before exchanging the value
temp=s1;
s1=s2;
s2=temp;
cout<<"after exchange:"<<s1<<s2<<endl;// display after exchanging the value
return 0;
}
Output:
before exchange:sanran
after exchange:ransan