C Program to Swap Two Strings

Write a program in c to swap the content of two strings

  • Write a C program to swap two strings using strcpy.
  • C Program to swap two strings without using extra memory

Given two strings, we have to swap the content of strings. For example, If firstString = “Apple” and secondString = “Banana” then after swapping firstString = “Banana” and secondString = “Apple”. We can either use strcpy to swap two strings using a temporary string or define user defined function to swap two strings.

C program to swap strings using strcpy function

In this program, we first take two string as input from user using gets function. We use a temporary character array tempString to temporarily store a string while swapping content. This program call strcpy function three times.

Algorithm to swap two strings using strcpy

This algorithm of swapping string is similar to the algorithm of swapping integers using a temporary variable. Let firstString and secondString are two input strings and tempString is a temporary string whose size is equal to or more than the size of firstString.

  • Copy firstString’s content into tempString using strcpy.
  • Copy secondString’s content into firstString using strcpy.
  • Copy tempString’s content into secondString.

C Program to Swap Two Strings

/*
* C Program to swap two strings
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
 
int main(){
    char firstString[100], secondString[100], tempString[100];
     
    printf("Enter first String \n");
    gets(firstString);
    printf("Enter second String \n");
    gets(secondString);
    /* Swap strings using a temporary array */
    /* Copy firstString into tempString */   
    strcpy(tempString, firstString);
    /* Copy secondString into firstString */
    strcpy(firstString, secondString);
    /* Copy tempString back to secondString*/
    strcpy(secondString, tempString);
    printf("After Swapping\n");
    printf("First String: %s\nSecond String: %s", firstString, secondString);
 
    getch();
    return 0;
}

Program Output

Enter first String
Apple
Enter second String
Banana
After Swapping
First String: Banana
Second String: Apple

C Program to swap two strings without using extra memory

In this program, we don’t use any temporary character array for swapping. We swap the characters of both input strings one by one from index 0 till end of the smaller string and then copy remaining characters of the bigger string.

C Program to swap two strings without using extra memory

/*
* C Program to swap two strings using function
*/
#include <stdio.h>
#include <conio.h>
 
void swapStrings(char *firstString, char *secondString);
int main(){
    char firstString[100], secondString[100];
    printf("Enter first String \n");
    gets(firstString);
    printf("Enter second String \n");
    gets(secondString);
     
    swapStrings(firstString, secondString);
    printf("After Swapping\n");
    printf("First String: %s\nSecond String: %s", firstString, secondString);
 
    getch();
    return 0;
}
 
/*
 * Swaps two passed strings 
 */
void swapStrings(char *firstString, char *secondString){
    if(firstString == NULL || secondString == NULL)
        return;
    /* Initialize it to first character index of both string */
    int firstStringIndex = 0, secondStringIndex = 0;
    char temp;
    /* Starting from index 0, keep on swapping characters 
     using a temporay char variable temp*/
    while(firstString[firstStringIndex] != '\0' && secondString[secondStringIndex] != '\0') {
        temp = firstString[firstStringIndex];
        firstString[firstStringIndex] = secondString[secondStringIndex];
        secondString[secondStringIndex] = temp;
        firstStringIndex++;
        secondStringIndex++;
    }
    if(firstString[firstStringIndex] == '\0'){
        /* If firstString ends before secondString, copy the remaining 
          characters of secondString into firstString */
        firstString[firstStringIndex++] = secondString[secondStringIndex];
        secondString[secondStringIndex++] = '\0';
        while(secondString[secondStringIndex] != '\0'){
            firstString[firstStringIndex++] = secondString[secondStringIndex++];
        }
        firstString[firstStringIndex] = '\0';
    } else {
        /* If secondString ends before firstString, copy the remaining 
          characters of firstString into secondString */
        secondString[secondStringIndex++] = firstString[firstStringIndex];
        firstString[firstStringIndex++] = '\0';
        while(firstString[firstStringIndex] != '\0'){
            secondString[secondStringIndex++] = firstString[firstStringIndex++];
        }
        secondString[secondStringIndex] = '\0';
    }
}
</conio.h></stdio.h>

Program Output

Enter first String
TechCrashCourse
Enter second String
CProgramming
After Swapping
First String: CProgramming
Second String: TechCrashCourse