Java Program to Swap Two Strings Without Using Third Variable

In this quick article, we will see how to write a Java program to swap two strings with or without using a third variable. First, we will see how to write a Java program to swap two strings using a third variable, and then we will see how to write a Java program to swap two strings without using a third variable.

Java Program to Swap Two Strings With a Third Variable

Using a third variable is the simplest and most intuitive way to swap two strings. Here's how you can do it:

Java Code

package com.java.tutorials.programs;

public class SwapTwoStrings {

    public static void main(String[] args) {

        String s1 = "java";
        String s2 = "guides";

        System.out.println("Before swapping two strings:");
        System.out.println("s1 => " + s1);
        System.out.println("s2 => " + s2);

        // Using a third variable to swap the strings
        String temp;
        temp = s1; // store s1 in temp
        s1 = s2;   // assign s2 to s1
        s2 = temp; // assign temp (original s1) to s2

        System.out.println("After swapping two strings:");
        System.out.println("s1 => " + s1);
        System.out.println("s2 => " + s2);
    }
}

Output

Before swapping two strings:
s1 => java
s2 => guides
After swapping two strings:
s1 => guides
s2 => java

Explanation

  1. Initial Values: The strings s1 and s2 are initially assigned the values "java" and "guides", respectively.
  2. Swapping:
    • The value of s1 is stored in a temporary variable temp.
    • The value of s2 is then assigned to s1.
    • Finally, the value stored in temp (which is the original value of s1) is assigned to s2.
  3. Result: After swapping, s1 becomes "guides" and s2 becomes "java".

Java Program to Swap Two Strings Without Using a Third Variable

Swapping two strings without using a third variable can be done by utilizing string concatenation and substring operations. Here's how:

Java Code

package com.java.tutorials.programs;

/**
 * Java Program to Swap Two Strings Without Using a Third Variable
 * @author Ramesh Fadatare
 *
 */
public class SwapTwoStrings {

    public static void main(String[] args) {

        String s1 = "java";
        String s2 = "guides";

        System.out.println("Before swapping two strings:");
        System.out.println("s1 => " + s1);
        System.out.println("s2 => " + s2);

        // Step 1: Concatenate s1 and s2 and store the result in s1
        s1 = s1 + s2; // "javaguides"

        // Step 2: Extract the initial value of s1 from the concatenated string
        s2 = s1.substring(0, s1.length() - s2.length()); // "java"

        // Step 3: Extract the initial value of s2 from the concatenated string
        s1 = s1.substring(s2.length()); // "guides"

        System.out.println("After swapping two strings:");
        System.out.println("s1 => " + s1);
        System.out.println("s2 => " + s2);
    }
}

Output

Before swapping two strings:
s1 => java
s2 => guides
After swapping two strings:
s1 => guides
s2 => java

Explanation

  1. Initial Values: The strings s1 and s2 are initially assigned the values "java" and "guides", respectively.
  2. Swapping Without Third Variable:
    • Step 1: Concatenate s1 and s2, and store the result in s1. Now, s1 contains "javaguides".
    • Step 2: Extract the original value of s1 (which is "java") by taking the substring of s1 from the beginning to the length of the original s1. This value is now stored in s2.
    • Step 3: Extract the original value of s2 (which is "guides") by taking the substring of s1 starting from the length of the original s1 to the end. This value is now stored in s1.
  3. Result: After swapping, s1 becomes "guides" and s2 becomes "java".

Conclusion

These two approaches demonstrate how to swap two strings in Java, both with and without using a third variable. The first method, using a temporary variable, is straightforward and easy to understand. The second method, which avoids using a third variable, is a bit more advanced and showcases the power of string manipulation in Java.

Comments