Write a C++ program to swap two strings, where each strings are entered by the user.

Program Code:
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{

char str1[20], str2[20], temp[20];
int i=0, j=0, k=0;
cout<<"Enter the First String : ";
gets(str1);
cout<<endl;
cout<<"Enter the Second String : ";
gets(str2);
cout<<endl;
while(str1[i]!='\0')
{
temp[j]=str1[i];
i++;
j++;
}
temp[j]='\0';
i=0, j=0;
while(str2[i]!='\0')
{
str1[j]=str2[i];
i++;
j++;
}
str1[j]='\0';
i=0, j=0;
while(temp[i]!='\0')
{
str2[j]=temp[i];
i++;
j++;
}
str2[j]='\0';
cout<<"Strings after swapping are : ";
cout<<endl;
cout<<"First String = "<<str1;
cout<<endl;
cout<<"Second String = "<<str2;
return 0;
}

The program output is tested on www.jdoodle.com
Output:

Enter the First String : Suruchika
Enter the Second String : Mukesh
Strings after swapping are : 
First String = Mukesh
Second String = Suruchika


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: