Java program to swap two strings without using third user defined variable

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

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

The idea is to string concatenation and substring() method to perform this operation. The substring() method comes in two forms as listed below: 

  • substring(int beginindex): 
  • substring(int beginindex,int endindex) This function will return Substring of the calling string ranging from the beginindex(inclusive) and ending at the endindex(exclusive) passed as argument to the present 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());
// 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

 

Submit Your Programming Assignment Details