Swap the two variable values without taking third variable?
Swap
the two variable values without taking third variable?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Ashutosh ChaturvediPosted Apr 5, 2013, 7:46 PM
I think
jignesh's first ans is best instead of using arithmatic operators.
Jignesh TrivediPosted Feb 11, 2013, 7:19 AM
you may use bitwise exclusive or operator to swap two number
example.
int x = 10;
int y = 12;
x ^= y;
y ^= x;
x ^= y;
you may also swap string without use third string
string a = "jignesh";
string b = "Trivedi";
a += b;
b = a.Substring(0, a.Length - b.Length);
a = a.Substring(b.Length);
hope this will help you.
Rishikesh Kumar SinghPosted Feb 11, 2013, 6:50 AM
a=a+b;
b=a-b;
a=a-b;
Now print the value of a and b....values have been swaped..
If it helps you then mark it as an accepted answer otherwise let me know..i will send you a small application implementing the same...