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
Post A Comment:
0 comments: