SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Swapping Two Strings Without Third Variable Program in Java and Python

25 April 2013

Java and Python program to swap two strings without using a third variable by using concatenation and substring slicing.

Question:

Write a program to input two strings and swap their values without using any third or temporary variable.

The program should display both strings before swapping and after swapping. Use string concatenation and substring operations to perform the swap.

Example

INPUT: First string: Java For Second string: School is Fun OUTPUT: Strings Before Swapping: 1st String = Java For 2nd String = School is Fun Strings After Swapping: 1st String = School is Fun 2nd String = Java For

Algorithm:

Step 1: Start.

Step 2: Create a Scanner object to accept input from the user.

Step 3: Accept the first string and store it in s1.

Step 4: Store the length of s1 in len1.

Step 5: Accept the second string and store it in s2.

Step 6: Display both strings before swapping.

Step 7: Join both strings and store the result in s1 by writing s1 = s1 + s2.

Step 8: Extract the original first string from the joined string using s1.substring(0, len1) and store it in s2.

Step 9: Extract the original second string from the joined string using s1.substring(len1) and store it in s1.

Step 10: Display both strings after swapping.

Step 11: Stop.

Explanation:

This program swaps the values of two string variables without declaring a third temporary variable. The method works by joining the two strings together and then cutting the joined string into two parts. To do this correctly, the program must remember the length of the first string before the joining step. This length is stored in len1.

Suppose s1 contains Java For and s2 contains School is Fun. The length of the first string is 8, including the space between the words. When the statement s1 = s1 + s2 is executed, s1 becomes Java ForSchool is Fun. The two original strings are now present inside one combined string. No extra variable has been used to store either original string separately.

The next step is to rebuild the two variables in swapped order. The original first string is from index 0 up to, but not including, index len1. Therefore s1.substring(0, len1) gives Java For, and this value is assigned to s2. After this step, the second variable has received the original value of the first variable.

Finally, s1.substring(len1) gives the part of the combined string starting from index len1 to the end. This part is the original second string, School is Fun. It is assigned back to s1. Now s1 contains the old value of s2, and s2 contains the old value of s1. Thus the strings are swapped using only concatenation, length and substring operations.

Java Program:

Java
/**
* The class Swap_Strings takes two strings as input and swaps
* their values without using a third variable.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;

class Swap_Strings
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the 1st String : ");
        String s1 = sc.nextLine();
        int len1 = s1.length();

        System.out.print("Enter the 2nd String : ");
        String s2 = sc.nextLine();

        System.out.println("-------------------------------");
        System.out.println("Strings Before Swapping : ");
        System.out.println("1st String = " + s1);
        System.out.println("2nd String = " + s2);

        // Swapping process begins.
        s1 = s1 + s2;
        s2 = s1.substring(0, len1);
        s1 = s1.substring(len1);
        // Swapping process ends.

        System.out.println("-------------------------------");
        System.out.println("Strings After Swapping : ");
        System.out.println("1st String = " + s1);
        System.out.println("2nd String = " + s2);
    }
}

Equivalent Python Program:

Python
# Program to swap two strings without using a third variable.

s1 = input("Enter the 1st String : ")
len1 = len(s1)

s2 = input("Enter the 2nd String : ")

print("-------------------------------")
print("Strings Before Swapping : ")
print("1st String =", s1)
print("2nd String =", s2)

# Swapping process begins.
s1 = s1 + s2
s2 = s1[:len1]
s1 = s1[len1:]
# Swapping process ends.

print("-------------------------------")
print("Strings After Swapping : ")
print("1st String =", s1)
print("2nd String =", s2)

Output:

Enter the 1st String : Java For Enter the 2nd String : School is Fun ------------------------------- Strings Before Swapping : 1st String = Java For 2nd String = School is Fun ------------------------------- Strings After Swapping : 1st String = School is Fun 2nd String = Java For

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →