For the record, you should just swap using a third string. Other methods are inefficient.
In pseudocode:
// We want to swap these two.
string1
string2
length1 = string1.length
length2 = string2.length
string1 = string1 + string2
string2 = first length1 characters from string1
string1 = last length2 characters from string1
To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;
a ^= b; b ^= a; a ^= b;
There are two ways in which you can swap without a third variable. 1. Using xor operation swap( int *a, int *b) { *a = *a ^ *b; *b = *a ^ *b; *a = *a ^ *b; } 2. Using addition and subtraction swap( int *a, int *b) { *a = *a + *b; *b = *a - *b; *a = *a - *b; } }
a=a^b; b=a^b; a=a^b;
Consider the following declarations:int x = 0;int y = 1;In order to swap the values, we need to use a temporary variable:int t = x;x = y;y = t;However, it is possible to swap the values without using a third variable:x ^= y ^= x ^= y;
swap (int *pa, int *pb) { *pa ^= *pb; *pa ^= *pa; *pa ^= *pb; }
Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)
The required c program is given below /*Swapping(interchange) the two entered numbers*/ #include<stdio.h> main() { /*Without using third variable*/ int a,b,t; printf("Enter a:"); scanf("%d",&a); printf("Enter b:"); scanf("%d",&b); a=a+b; b=a-b; a=a-b; printf("\n After swapping without using third variable"); printf("\na=%d\nb=%d",a,b); }
By using the algorithm of bitwise EORing (Exclusive ORing) the numbers together:If the two numbers are X and Y, then to swap them:X = X EOR YY = Y EOR XX =X EOR Ywill swap them.With knowledge of that algorithm, one then uses the syntax of Javascript to implement it.
with a loop. len= strlen (s); p= s; q= s+len-1; while (p<q) { (swap) ++p; --q; }
By using a third temporary variable. $tmp = $a; $a = $b; $b = $tmp;
To swap two numbers N1 and N2, using a third variable T... T = N1; N1 = N2; N2 = T;