how to swap two strings without using any third variable ?

Answers were Sorted based on User's Feedback



how to swap two strings without using any third variable ?..

Answer / chitranshu asthana

char* s1 = "Hello";
char* s2 = "World";
s1 = (char*)((int)s1 + (int)s2);
s2 = (char*)((int)s1 -(int) s2);
s1 = (char*)((int)s1 - (int)s2);
printf("%s:%s", s1, s2);

Is This Answer Correct ?    91 Yes 13 No

how to swap two strings without using any third variable ?..

Answer / santosh panigrahy

int main()
{
char *a = "hello";
char *b = "world";
a = a + (a-b);
b = a - (a-b)/2;
a = a - (a-b)*2;
printf(" a = %s b = %s ",a,b);
return 0;
}

Is This Answer Correct ?    26 Yes 5 No

how to swap two strings without using any third variable ?..

Answer / ankit sharma

ANSWER 4 IS BEST AND EASIEST SOLUTION.....THANKS BUDDY
PLEASE DON'T REFER ABOVE THREE SOLUTION BCOZ THEY ARE FOR INTEGER NOT FOR STRING.
I RATED 4TH SOLUTION IS 5/5.

Is This Answer Correct ?    16 Yes 4 No

how to swap two strings without using any third variable ?..

Answer / ashutosh

say a = 10 and b = 20;
b = a + b;// b= 30
a = b - a;// a= 20
b = b - a;// b= 10

so now a = 20 b= 10

Is This Answer Correct ?    27 Yes 16 No

how to swap two strings without using any third variable ?..

Answer / ruchi

Hi ashutosh,
This technique is used to swap numeric values without using
3rd vaariable not for strings .
We can do it by using binary shift operators.

Is This Answer Correct ?    8 Yes 7 No

how to swap two strings without using any third variable ?..

Answer / mathias karlsson

X = X & Y
Y = X.Substring(&H0, X.Length - Y.Length)
X = X.Substring(X.Length - Y.Length, Y.Length)

Is This Answer Correct ?    0 Yes 0 No

how to swap two strings without using any third variable ?..

Answer / rohit kapade

bool SwapString(char **s1,char **s2)
{
//char *temp = NULL;
//temp = *s1;
//*s1 = *s2;
//*s2 = temp;
*s1 = (char*)((int)*s1 + (int)*s2);
*s2 = (char*)((int)*s1 - (int)*s2);
*s1 = (char*)((int)*s1 - (int)*s2);
return true;
}

Is This Answer Correct ?    3 Yes 3 No

how to swap two strings without using any third variable ?..

Answer / doesntmatter

" PLEASE DON'T REFER ABOVE THREE SOLUTION BCOZ THEY ARE FOR
INTEGER NOT FOR STRING. "

lol

(c) string -> char * -> pointer -> integer.

Is This Answer Correct ?    0 Yes 1 No

how to swap two strings without using any third variable ?..

Answer / kollu.praveen

var1="String1"
var2="String2"
var1=var1+var2
var2=mid(var1,1,Len(var1)-Len(var2))
print var2
var1=mid(var1,(Len(var1)-Len(var2)+1),(Len(var2)+1))
print var1

Is This Answer Correct ?    0 Yes 2 No

how to swap two strings without using any third variable ?..

Answer / rohit kapade

bool SwapString(char **s1,char **s2)
{
char *temp = NULL;
temp = *s1;
*s1 = *s2;
*s2 = temp;
//strcpy(temp,s1);
//strcpy(s1, s2);
//strcpy(s2, temp);
return true;
}

Is This Answer Correct ?    0 Yes 6 No

Post New Answer

More C++ General Interview Questions

What is extern c++?

0 Answers  


What is the full form of c++?

0 Answers  


Reads in the size of a square from the screen; 2. Prints a hollow square of that size out of “-“, “|” and blanks on screen; 3. Prints the same hollow square onto a text file. Your program should work for squares of all side sizes between 1 and 20. --- │ │ │ │ │ │ ---

0 Answers  


Explain about Garbage Collector?

0 Answers  


What are destructors?

0 Answers  






When should we use container classes instead of arrays?

0 Answers  


What is your strongest programming language (Java, ASP, C, C++, VB, HTML,C#, etc.)?

24 Answers   Infosys, Microsoft, TCS,


What is the use of volatile keyword in c++? Give an example.

1 Answers  


Is c the same as c++?

0 Answers  


Which software is used for c++ programming?

0 Answers  


How would you obtain segment and offset addresses from a far address of a memory location?

0 Answers  


Draw a flow chart and write a program for the difference between the sum of elements with odd and even numbers. Two dimensional array.

0 Answers  


Categories