Java Software Programming Examples For Beginners / Interview: How To Swap Integer (and String) Variables Without Using a Temporary Variable
Say x & y are the integer variables. The challenge is to swap them without using a temporary variable.
The solution is as simple as the problem itself:
x = x + y;
y = x - y;
x = x - y;
Update 1: Jack and Alexey pointed out a typo in my solution which has since been corrected. Please see their comments below.
The simplicity of the solution appeals to me. It clearly demonstrates the meaning of assignment operator ( "=" ).
What if they are Strings?
Note: You can use String methods in Java API.
Here is the solution:
x = x + y;
y = x.substring(0, x.indexOf(y));
x = x.substring(x.indexOf(y) + y.length());
Can you see the similarity?
Can you provide a simpler solution to either of the above?
Update 2: Robert just did (see his comment below). He pointed out that the String solution doesn't work when one string is contained in another. His solution is:
x = x + y;
y = x.substring(0, x.length() - y.length());
x = x.substring(y.length());
Filed under How To, Java Software, Tech Note |
|
RSS 2.0 |
Trackback this Article
|
Email this Article
You may also like to read |






































October 30th, 2005 at 2:53 am
x : 1 y : 2
x = x + y;
x : 3 y : 2
x = x - y;
x : 1 y : 2
y = x - y;
x : 1 y : -1
October 30th, 2005 at 3:04 am
x = x+y;
y = x-y;
x = x-y;
October 30th, 2005 at 8:36 am
Maybe
x = x + y;
y = x - y;
x = x - y;
October 30th, 2005 at 9:24 am
@Jack & Alexey
Oops!
Thanks for the catch. Corrected.
Just checking if you are attentive
October 30th, 2005 at 9:25 am
BTW: I actually asked this question today while interviewing candidates.
October 31st, 2005 at 8:15 am
The string sample will fail if the string x contains the string y. For example,
String x = “Hello World”;
String y = “Hello”;
Step 1: x = x + y;
x = “Hello WorldHello”
Step 2: y = x.substring(0, x.indexOf(y));
x.indexOf(y) = 0, so y = “”;
Step 3: x = x.substring(x.indexOf(y) + y.length());
x.indexOf(y) = 0 and y.length = 0, so x = “Hello WorldHello”
You can fix this by using lastIndexOf() method in String like
x = x + y;
y = x.substring(0, x.lastIndexOf(y));
x = x.substring(x.indexOf(y) + y.length());
or you could eliminate the indexOf() calculations altogether and just use the lengths of the strings
x = x + y;
y = x.substring(0, x.length() - y.length());
x = x.substring(y.length());
October 31st, 2005 at 8:53 am
@Robert
I like that. It looks cleaner.
November 9th, 2005 at 2:08 am
Unfortunately, the code to exchange integer values will fail for values that cause over- or underflow.
However, you can use exclusive-or (xor), which is like add without carry. The following unobvious code exchanges all int values.
x = x ^ y;y = x ^ y;
x = x ^ y;
September 28th, 2006 at 6:10 am
im coding a anagram word game to swap letters from one label to another (up to the down) by using a threeBottons class how should i use the string and char code? (the second character of label one should place to the second character of label two) i need that much help thanks
November 29th, 2007 at 8:30 am
import java.io.*;
class ab
{
public static void main(String arg[]) throws IOException
{
int x=6;
if(x=6)
{
System.out.println(”Hi…”);
}
}
}
the question is .. Is it possible to run this program with out changing if(x=6)
February 26th, 2008 at 4:34 am
if(x > y)
{
x = x- y;
y = y + x;
x = y - x;
}
else
{
y = y - x;
x = x + y;
y = x - y;
}