codescracker
c

C Program to Swap Two Strings



« Previous Program Next Program »

In this article, you will learn and get code on swapping of two given strings in C programming using following approaches:

Let's first create a program that uses a library function. Then later on, you will get the program without any library function to swap two strings.

Using Function

This program uses a function strcpy(). This function takes two argument of char type. The value of second argument gets copied to the first one. For example, if there are two char type variables say num1 and num2. And let's suppose, num2 holds its value as codescracker. Then the following statement:

strcpy(num1, num2);

copies the value of num2 to num1. Now both the variables, holds the same value, that is codescracker. The function strcpy() is defined in string.h header file. Now let's move on to the program:

// C Program to Swap Two Strings using Function
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char str1[50], str2[50], temp[50];
    printf("Enter the First String: ");
    gets(str1);
    printf("Enter the Second String: ");
    gets(str2);
    printf("\nString before Swap:\n");
    printf("First String = %s\tSecond String = %s", str1, str2);
    strcpy(temp, str1);
    strcpy(str1, str2);
    strcpy(str2, temp);
    printf("\n\nString after Swap:\n");
    printf("First String = %s\tSecond String = %s", str1, str2);
    getch();
    return 0;
}

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

c program swap two strings

Now enter the first string say codes and then enter second string say cracker to perform the swap operation of given two strings as shown in the following output:

swap two string c

As you can see from the above program, the main code of swapping the two strings are:

strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);

If user enters codes and cracker as the values of first and second string. That is, str1=codes and str2=cracker.

Then after executing the first statement, that is, strcpy(temp, str1);
the value of str1 gets copied to temp. Therefore, now temp=codes.

And after executing the second statement, that is, strcpy(str1, str2);
the value of str2 gets copied to str1. Therefore, now str1=cracker

And finally after executing the third statement, that is, strcpy(str2, temp);
the value of temp gets copied to str2. Therefore, now str2=codes

As you can see, after executing the three statements given above, str1 holds the value that str2 previously has and vice-versa.

Without using Library Function

This program swaps two given strings without using any type of library function.

// Swap Two Strings without using Library Functions
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    char str1[50], str2[50], temp[50];
    int i=0;
    printf("Enter the First String: ");
    gets(str1);
    printf("Enter the Second String: ");
    gets(str2);
    printf("\nString before Swap:\n");
    printf("First String = %s\tSecond String = %s", str1, str2);
    while(str1[i]!='\0')
    {
        temp[i] = str1[i];
        i++;
    }
    temp[i] = '\0';
    i=0;
    while(str2[i]!='\0')
    {
        str1[i] = str2[i];
        i++;
    }
    str1[i] = '\0';
    i=0;
    while(temp[i]!='\0')
    {
        str2[i] = temp[i];
        i++;
    }
    str2[i] = '\0';
    printf("\n\nString after Swap:\n");
    printf("First String = %s\tSecond String = %s", str1, str2);
    getch();
    return 0;
}

It will produce the same output as of previous program. In this program, we've first copied all the characters of first string say str1 to a variable say temp in character-by-character manner. Then copied all the characters of second string say str2 to first one say str1. Finally copied all characters of temp to second string say str2.

Note - Never forgot to initialize a null terminated character (\0) as last character to all the variable such as str1, str2 and temp.

For example, if user enters codes and cracker as two strings. Then codes gets initialized to num1 variable in a way that:

Same happened with second string. Now with following block of code:

while(str1[i]!='\0')
{
    temp[i] = str1[i];
    i++;
}

Because the initial value of i is 0, therefore all the 5 characters of num1 variable gets initialized to temp variable one by one in a way that:

Now temp = codes. Again initialize the value of i with 0 and do for the next one. In this way, the swapping of two strings gets performed.

Same Program in Other Languages


« Previous Program Next Program »