C Program to Swap Two Strings

In this C program, we will swap two strings using strcpy function and 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.

#include <stdio.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);
   
    strcpy(tempString, firstString);
   
    strcpy(firstString, secondString);
    
    strcpy(secondString, tempString);
    printf("After Swapping\n");
    printf("First String: %s\nSecond String: %s", 
        firstString, secondString);

    return 0;
}
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.

#include <stdio.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);

    return 0;
}

void swapStrings(char *firstString, char *secondString){
    if(firstString == NULL || secondString == NULL)
        return;
        
    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'){
        firstString[firstStringIndex++] = secondString[secondStringIndex];
        secondString[secondStringIndex++] = '\0';
        while(secondString[secondStringIndex] != '\0'){
            firstString[firstStringIndex++] = secondString[secondStringIndex++];
        }
        firstString[firstStringIndex] = '\0';
    } else {
        secondString[secondStringIndex++] = firstString[firstStringIndex];
        firstString[firstStringIndex++] = '\0';
        while(firstString[firstStringIndex] != '\0'){
            secondString[secondStringIndex++] = firstString[firstStringIndex++];
        }
        secondString[secondStringIndex] = '\0';
    }
}
Output
Enter first String
TechCrashCourse
Enter second String
CProgramming
After Swapping
First String: CProgramming
Second String: TechCrashCourse

Related Topics
C program to copy a string
C program to compare two strings
C program to concatenate strings
C program to convert string to integer
C program to remove extra spaces from string
C program to sort characters of a string
C program to check a number is palindrome or not
List of all C programs