How to swap two strings without using third variable in java / How to swap two strings without using a temporary variable in java

Problem
Suppose there are 2 strings and their values need to be interchanged. String variables are declared as

String stringOne = “This is “;
String stringTwo = “codippa.com “;

and the requirement is to interchange(or swap) their values such that their values become

stringOne = “codippa.com “;
stringTwo = “This is “;

A typical approach to interchange the values of these string variables is by using a third variable as

String temp = “”;
// store value of first string in temp
temp = stringOne;
// store the value of second string into first string
stringOne = stringTwo;
// store the value stored in temp into second string
stringTwo = temp;

What if the string values need to be interchanged without using the temporary(or third) variable. There are a couple of methods to do this and this post will guide through these methods.

Method 1 : Concatenate the strings using a separator

Concatenate both the strings to be swapped by a separator string and then split the resultant string over the same separator using split method of java.lang.String class to get an array. Now the value at index 1 is stored in the first string and the value at index 0 is stored in second string. In this way, the strings are swapped. Following example demonstrates this.

public class StringSwapper {
	public static void main(String[] args) {
		String stringOne = "This is ";
		String stringTwo = "codippa.com ";
		System.out.println("Before swapping ::");
		System.out.println("String One : " + stringOne);
		System.out.println("String Two : " + stringTwo);
		// concatenate two strings with a separator string
		stringOne = stringOne + "!@#" + stringTwo;
		// split the combined string on separator
		String[] stringArr = stringOne.split("!@#");
		// assign value at first index to second string
		stringTwo = stringArr[0];
		// assign value at second index to first string
		stringOne = stringArr[1];
		System.out.println("After swapping ::");
		System.out.println("String One : " + stringOne);
		System.out.println("String Two : " + stringTwo);
	}
}

Output

Before swapping ::
String One : This is
String Two : codippa.com
After swapping ::
String One : codippa.com
String Two : This is

Method 2 : Concatenate the strings without any separator

This method utilizes both the versions of  substring methods of java.lang.String class which are :

  1. substring(int beginIndex, int startIndex) : Returns the substring starting with the begin index till end index – 1.
  2. substring(int beginIndex) : Returns the substring starting with the given index till the end of the string.

Calculate the length of the first string using length method of java.lang.String class. Concatenate the two strings and assign the result to the variable which holds the first string. Now substring the combined string from start to the length of the first string using the first version of substring method above. This will be the same as the first string but we will assign it to the second string.
Now again substring the combined string with the length of the string as start index using the second version of substring method. This will give the same string as second string but we will assign it to the first string.  Code follows :

public class StringSwapper {
	public static void main(String[] args) {
		String stringOne = "This is ";
		// calculate length of first string
		int firstStringLength = stringOne.length();
		String stringTwo = "codippa.com ";
		System.out.println("Before swapping ::");
		System.out.println("String One : " + stringOne);
		System.out.println("String Two : " + stringTwo);
		// concatenate two strings
		stringOne = stringOne + stringTwo;
		// substring the combined string from the beginning till the length of
		// first string. This will be same as first string.
		// Assign it to second string
		stringTwo = stringOne.substring(0, firstStringLength);
		// substring the combined string from the length of first string till
		// the end. This will the same as second string.
		// Assign it to first string
		stringOne = stringOne.substring(firstStringLength);
		System.out.println("After swapping ::");
		System.out.println("String One : " + stringOne);
		System.out.println("String Two : " + stringTwo);
	}
}

Output

Before swapping ::
String One : This is
String Two : codippa.com
After swapping ::
String One : codippa.com
String Two : This is

Let’s tweak in :

  1. Method 1 will not work if any of the strings will have the separator used to combine the strings. Choose a string whose probability of occurrence in any string is very low such as “!@#” used in the example.
  2. A variant of Method 2 is to combine the strings together and then use the substring method of java.lang.String class which takes 2 arguments with start index as 0 and end index as stringOne.length() - stringTwo.length().
    Explanation:
    Length of first string after concatenation will be the total length of both the strings and length of string two will be the length of right half of the string. Performing a substring as substring(0, stringOne.length() - stringTwo.length()) will give the left half of the string. This will be the first string which can be assigned to the second string to perform swapping.

 

Leave a Reply