Java Program to Swap Two Strings

In this tutorial, we will write a Java program to swap two strings without using the third or temporary variable.

Before going to this approach, we will first learn how to swap two strings using the third variable in Java. This is a very simple approach to swap two strings in Java.

Swap Two Strings in Java using Third Variable


Let’s write a simple Java program to swap two strings using a third variable.

Example 1:

package stringPrograms;
public class Swapping {
public static void main(String[] args) 
{
// Create two string literals.	
  String s1 = "Love";
  String s2 = "You";
  System.out.println("Before swapping, s1 = " +s1+ ", s2 = "+s2 );

// Swapping two strings using third variable temp. 
   String temp = s1;
   s1 = s2;
   s2 = temp;
   System.out.println("After swapping, s1 = " +s1+ ", s2 = "+s2 );
  }
}
Output:
      Before swapping, s1 = Love, s2 = You
      After swapping, s1 = You, s2 = Love

Java Program to Swap two Strings without using Third Variable


Let’s create a Java program to swap two strings without using a third or temp variable. There are the following steps to swap two strings without using the third variable.

Algorithm:

Step 1: Define two strings, s1 and s2 to swap.

Step 2: Display strings before swapping.

Step 3: Concatenate given two strings (s1 + s2) and store it into s1.

Step 4: Extract second string s2 from updated string s1 using substring() method and store it into string s2.

Step 5: Extract string s1 from updated string s1 using substring () method till the end of the string. Then, store it into string s1.

Step 6: Display both strings after swapping.

Example 2:

package stringPrograms;
public class Swapping {
public static void main(String[] args) 
{
// Create two string literals.	
  String s1 = "Love";
  String s2 = "You";
  System.out.println("Before swapping, s1 = " +s1+ ", s2 = "+s2 );

// Concatenate both the string s1 and s2 and store it in s1    
   s1 = s1 + s2;    
// Extract s2 from updated string s1.    
   s2 = s1.substring(0, (s1.length() - s2.length()));    
  
// Extract s1 from updated string s1    
   s1 = s1.substring(s2.length());   
   System.out.println("After swapping, s1 = " +s1+ ", s2 = "+s2 );
  }
}
Output:
      Before swapping, s1 = Love, s2 = You
      After swapping, s1 = You, s2 = Love

Explanation:

1. In this program, we have created two string literals, s1 and s2 with values “Love” and “You”. After creating them, we have displayed before swapping on the console.

2. Then, we have concatenated both strings using + operator and stored it into string s1.

3. We have extracted string s2 from updated string s1 using substring() method of String class. The substring() method returns a string that is a substring of this string.

The substring starts at the specified beginIndex and goes to the character at index endIndex – 1. Thus, the length of the substring is endIndex-beginIndex. The general syntax is as:

String.substring(int beginIndex, int endIndex)

The first parameter, beginIndex, specifies the beginning index that is inclusive. The second parameter, endIndex, specifies the ending index that is exclusive.

For examples:

  • “hamburger”.substring(4, 8) returns “urge”.
  • “smiles”.substring(1, 5) returns “mile”.

4. The statement s2 = s1.substring(0, (s1.length() – s2.length())); will return “Love” and store it in string s2.

This is because s1.length() return the length 7 and s2.length() return the length 3. Thus, LoveYou.substring(0, (7 – 3)) i.e. LoveYou.substring(0, 4) return “Love”.

5. At last, we have extracted string s1 from updated string s1 using substring() method and store it in string s1.

6. The statement s1 = s1.substring(s2.length()); will return “You” and store it into s1. This is because LoveYou.substring(3) return “You”.

7. At last, we have displayed both strings on the console after swapping without using third variable.

How to Swap two Numbers without using Third Variable?


Let’s write a Java program to swap two numbers without using the third variable.

Example 3:

package stringPrograms;
import java.util.Scanner;
public class Swapping {
public static void main(String[] args) 
{
// Create a Scanner class.	
   Scanner sc = new Scanner(System.in);
   int x, y;
   System.out.println("Enter your first number: ");
   x = sc.nextInt();
   System.out.println("Enter your second number: ");
   y = sc.nextInt();
   System.out.println("Before swapping, x = " +x+ ", y = " +y);
   x = x + y;
   y = x - y;
   x = x - y;
   System.out.println("After swapping, x = " +x+ ", y = " +y);
  }
}
Output:
      Enter your first number: 
      10
      Enter your second number: 
      30
      Before swapping, x = 10, y = 30
      After swapping, x = 30, y = 10

In this tutorial, you learned how to swap two strings without using the third or temp variable. We have also written a Java program to swap two strings with using the third variable that is a simple approach.

Hope that you will have understood the basic concepts of swapping two strings and two numbers without using the third variable. In the next, we will learn how to convert a string into int in Java with the help of example programs.
Thanks for reading!!!