- Home
- Course Category
- Course Result
- Course Detail
Hot Line: 0124-2383121
Course Introduction
Programs for practice
Following C++ program ask to the user to enter the two strings (you can enter any two name or two word), to swap the string, then display the result after the swapping the strings:
Source Code: Swap Two Strings
/* C++ Program - Swap Two Strings */
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int i=0, j=0;
char str1[20], str2[20], temp[20];
cout<<"Enter the First String : ";
gets(str1);
cout<<"Enter the Second String : ";
gets(str2);
cout<<"Strings before swapping are :\n";
cout<<"String 1 = "<<str1<<"\n";
cout<<"String 2 = "<<str2<<"\n";
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 : \n";
cout<<"String 1 = "<<str1<<"\n";
cout<<"String 2 = "<<str2<<"\n";
getch();
}
Output
Enter the First String : blazing
Enter the Second String :minds
Strings before swapping are :
String 1 = blazing
String 2 = minds
Strings after swapping :
String 1 = minds
String 2 = blazing