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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rishikesh Kumar SinghRank #1,250 · 1.4K reputationPosted Feb 11, 2013, 1:50 AM EST
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...
Jignesh TrivediRank #16 · 50.3K reputationPosted Feb 11, 2013, 2:19 AM EST
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.
Ashutosh Chaturvedi93 reputationPosted Apr 5, 2013, 3:46 PM EDT
I think
jignesh's first ans is best instead of using arithmatic operators.