You can't. While a string is a character array, an array is not necessarily a string. Treating arrays as if they were strings simply to swap them is madness.
The correct way to physically swap arrays A and B is to copy A to a new array, C, then copy B to A, then C to B. If the arrays are the same size this is not a problem. If they are different sizes, you can only swap them if they are dynamic (not static). This means you must reallocate them. To speed up the process, copy the smallest array to C, first.
A much better approach would be to point at the two arrays and swap the pointers instead.
To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;
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
void swap (int &pa, int &pb) { *pa ^= *pb; *pb ^= *pa; *pa ^= *pb; }
Although std::swap caters for all built-in types and is specialised to cater for all STL containers, C-style arrays and strings, the default implementation may not be efficient for all user-defined types. Although you can specialise std::swap to cater for user-defined types, this is not considered best practice. The correct way to implement your own swap function is to define it as a global friend function within the same namespace as the object you're swapping so that it can be found with ADL. The following shows an example implementation: class foo : public b1, public b2 { private: int m1; float m2; public: friend void swap (foo& a, foo& b) { // import swap for built-in types using std::swap; // always swap base class members first... swap (a::b1, b::b1); swap (a::b2, b::b2); // ...then swap the members swap (a.m1, b.m1); swap (a.m2, b.m2); } };
bubble_sort (int N, int *A) { int i; swap = 1; while (swap) { swap = 0; for (i=0; i<N-1; ++i) { if (A[i] > A[i+1]) { swap = 1; A[i] ^= A[i+1]; A[i+1] ^= A[i]; A[i] ^= A[i+1]; } } } }
void swap(int& a, int& b ) { a^=b^=a^=b; }
with a loop. len= strlen (s); p= s; q= s+len-1; while (p<q) { (swap) ++p; --q; }
void swap(int &x, int &y) { x ^= y ^= x; } - or - void swap(int &x, int &y) { int t = x; x = y; y = t; }
YES
You can swap two integers without temporary storage by bitwise exclusive-or'ing them in a specific sequence...a ^= b;b ^= a;a ^= b;
Do you mean how do you sort strings using bubble sort? void exch( std::string& a, std::string& b) { std::string tmp = a; a = b; b = tmp; } void bubble_sort( std::string A[], size_t size ) { size_t last_exch, left, right; while( size ) { for( left=0, right=1, last_exch=left; right<size; ++left, ++right) if( A[right]<A[left] ) exch( A[left], A[last_exch=right] ); size = last_exch; } } Clearly this is inefficient. A better approach would be to use an array of pointers to strings, and swap the pointers instead. The same can be said of any array of objects: use an array of pointers to the objects, never store the objects themselves in the array that is to be sorted.
void swap(int &a,int &b) { a=a+b; b=a-b; a=a-b; } int main(void) { int a,b; cout<<"Enter the value for a and b"; cin>>a>>b; cout<<"Before swapping"<<endl; cout<<"A= "<<a<<"B= "<<b; cout<<After swapping"<<endl; swap(a,b); cout<<"A= "<<a<<"B= "<<b; return 0; }