Skip to content
Related Articles
Open in App
Not now

Related Articles

Java Program to Swap two Strings Without Using any Third Variable

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 04 Aug, 2022
Improve Article
Save Article
Like Article

Given two string variables, a and b, your task is to write a Java Program to swap these variables without using any temporary or third variable. Use of library methods is allowed.

Examples: 

Input: a = "Hello"
       b = "World"

Output:
Strings before swap: a = Hello and b = World
Strings after swap: a = World and b = Hello

Method: In order to swap two string variables without using any temporary or third variable, the idea is to use string concatenation and substring() methods to perform this operation. The substring() method comes in two forms, as listed below: 

  • substring(int beginindex): This function will return the substring of the calling string starting from the index passed as an argument to this function till the last character in the calling string. 
  • substring(int beginindex, int endindex): This function will return the Substring of the calling string starting from the beginindex(inclusive) and ending at the endindex(exclusive) passed as an argument to this function. 
     

Algorithm: 

1) Append second string to first string and 
   store in first string:
   a = a + b

2) call the method substring(int beginindex, int endindex)
   by passing beginindex as 0 and endindex as,
      a.length() - b.length():
   b = substring(0,a.length()-b.length());

3) call the method substring(int beginindex) by passing 
   b.length() as argument to store the value of initial 
   b string in a
   a = substring(b.length());

Below is the implementation of the above approach:

Java




// Java program to swap two strings without using a temporary
// variable.
import java.util.*;
 
class Swap
{   
    public static void main(String args[])
    {
        // Declare two strings
        String a = "Hello";
        String b = "World";
         
        // Print String before swapping
        System.out.println("Strings before swap: a = " +
                                       a + " and b = "+b);
         
        // append 2nd string to 1st
        a = a + b;
         
        // store initial string a in string b
        b = a.substring(0,a.length()-b.length());
         
        // store initial string b in string a
        a = a.substring(b.length());
         
        // print String after swapping
        System.out.println("Strings after swap: a = " +
                                     a + " and b = " + b);       
    }   
}


Output

Strings before swap: a = Hello and b = World
Strings after swap: a = World and b = Hello

Time Complexity: O(n+m) where n and m are lengths of given strings.
Auxiliary Space: O(n+m)

This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!