Custom Search

Swap 2 variable content without Temporary variable

Whenever it comes to swap the content(integer, float data) of 2 variables in any programming languages we often create a temporary variables that will the temporary data before swapping. But I’ll show you a technique where you can swap without creating an extra temporary variable.





Suppose i have 2 variable “var1″ and “var2″

var1 = 9 and var2 = 4

Step 1: var1 = var1 + var2

So as per step1 now var1 = 13 and var2 = 4

Step 2: var2 = var1 - var2

So as per step2 now var2 = 9 and var1 = 13

Step 3: var1 = var1 - var2

So as per step3 now var2 = 9 and var1 = 4.

So happy coding :)

Custom Search

Posts

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

Why would you ever EVER want to use this technique? There’s no fathomable reason that you’d ever not want to use a temporary variable.

Hi Andy Lester,
This questions are basically used to test the logical thinking power of the programmer and is not to be practically used in programming. You will find this questions during interviews.

The method mentioned wont work because it doesn’t take into account overflow errors. Suppose for example if we had to swap two signed numbers 1 and 32767 on a system where int is 16 bits long. On such a system, the maximum value an int can store (INT_MAX) would typically be 32767.So when the first step var1+var2 is executed the result would be 32768, which would be larger than the maximum value that can be stored in an int variable. Consequently this value cant be stored in var1 and hence an overflow occurs. What happens after this is undefined. No matter what happens under the hood, the result is bound to be undefined and hence this method fails whenever an overflow occurs.

Hi Uma,
I appreciate your effort on pointing out the overflow errors. I know that this will give me an error when working with Prgramming Languages like C++, Java, C# where you have to define the datatype. But this will works well with programming language that implements loosely datatype technique like JavaScript, PHP etc as in this the variable datatype changes as per the content.

Swapping of Two String without using Temporary Variables and Avoiding The Creation of More Than 2 Values and Without using Array or Even explode/implode or using marker(separator) USING PHP:

<?php

$string1 = “Hello”;
$string2 = “Jun”;

$string1 = $string2 . $string1;
$string2 = substr( $string1 , strlen( $string2 ) , strlen($string1) );
$string1 = substr( $string1 ,0, strlen( $string1 ) - strlen($string2) );

echo “$string1″;
echo “$string2″;

?>

Another one is using exclusive-or:

1: x = x ^ y;
2: y = y ^ x;
3: x = x ^ y;

Let’s see: suppose x is 1001 and y is 1100:
1: x is 0101 and y is 1100
2: x is 0101 and y is 1001
3: x is 1100 and y is 1001

hi uma ……….
hats off to ur logic the ritesh provided method will not work for overflow and hence can be implemented in a range only

Leave a comment

(required)

(required)