codescracker
c++

C++ Program to Swap Two Strings



« Previous Program Next Program »


In this article, you will learn and get code to swap two strings entered by user using a C++ program. Here are the list of programs on swapping of two given strings:

Swap Two Strings using Library Function

To swap two strings in C++ programming, you have to ask from user to enter the two strings. The value of both strings gets stored in two variables say strOne (first string) and strTwo (second string). Now swap these two strings using a third variable say strTmp with the help of library function, strcpy() as shown in the program given below:

// C++ Swap Two Strings using Library Function
// ----codescracker.com----

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
    char strOne[50], strTwo[50], strTmp[50];
    cout<<"Enter the First String: ";
    gets(strOne);
    cout<<"Enter the Second String: ";
    gets(strTwo);
    cout<<"\nString before Swap:\n";
    cout<<"First String = "<<strOne<<"\tSecond String = "<<strTwo;
    strcpy(strTmp, strOne);
    strcpy(strOne, strTwo);
    strcpy(strTwo, strTmp);
    cout<<"\n\nString after Swap:\n";
    cout<<"First String = "<<strOne<<"\tSecond String = "<<strTwo;
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program swap two strings

Now supply two strings say codes as first and cracker as second string. Here is the sample run with these two strings as input:

swap two strings c++

The following block of code:

strcpy(strTmp, strOne);
strcpy(strOne, strTwo);
strcpy(strTwo, strTmp);

is responsible for swapping of two strings. For example, if user enters the same two strings as given in above program's sample run. Initially strOne=codes and strTwo=cracker. Now after executing the first statement (of above three lines of statement):

strcpy(strTmp, strOne);

the value of strOne gets copied to strTmp. So strTmp=codes. In similar way, after executing the following (second) statement:

strcpy(strOne, strTwo);

the value of strTwo gets copied to strOne. So strOne=cracker. And using the third statement, that is:

strcpy(strTwo, strTmp);

the value of strTmp gets copied to strTwo. So strTwo=codes. In this way swapping gets perfomed using strcpy() library function of C++.

Swap Two Strings without using Library Function

This program just copies the string manully (using user-based code), without using library function strcpy():

// C++ Swap Two Strings without using Library Function
// ----codescracker.com----

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    char strOne[50], strTwo[50], strTmp[50];
    int i=0;
    cout<<"Enter the First String: ";
    gets(strOne);
    cout<<"Enter the Second String: ";
    gets(strTwo);
    cout<<"\nString before Swap:\n";
    cout<<"First String = "<<strOne<<"\tSecond String = "<<strTwo;
    while(strOne[i]!='\0')
    {
        strTmp[i] = strOne[i];
        i++;
    }
    strTmp[i] = '\0';
    i=0;
    while(strTwo[i]!='\0')
    {
        strOne[i] = strTwo[i];
        i++;
    }
    strOne[i] = '\0';
    i=0;
    while(strTmp[i]!='\0')
    {
        strTwo[i] = strTmp[i];
        i++;
    }
    strTwo[i] = '\0';
    cout<<"\n\nString after Swap:\n";
    cout<<"First String = "<<strOne<<"\tSecond String = "<<strTwo;
    cout<<endl;
    return 0;
}

This program produces the same output as of previous program. In above program, instead of using strcpy() function to copy string, we've done the same thing using user-based code.

Note - To learn more about copy one string to other, refer to copy string program to get every required thing about the topic. There you will get the full detail about how the code given in previous program works.

Same Program in Other Languages


« Previous Program Next Program »