answersLogoWhite

0

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.

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

How do you write a program in C plus plus plus plus How do you write a program in C to swap two variables without using the third oneo swap two variables without using the third one?

To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;


Swap two string without using third one?

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


Coding in c plus plus to swap two nos using pointer?

void swap (int &pa, int &pb) { *pa ^= *pb; *pb ^= *pa; *pa ^= *pb; }


Wap in c plus plus for swapping contents of two variables using friend function?

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 sorting in c plus plus?

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]; } } } }


How to swap two numbers by call by reference in c plus plus?

void swap(int& a, int& b ) { a^=b^=a^=b; }


Can you win in Uno by using a Swap Hands card?

No, you cannot win in Uno by using a Swap Hands card.


How do you swap two numbers in c plus plus?

void swap(int &x, int &y) { x ^= y ^= x; } - or - void swap(int &x, int &y) { int t = x; x = y; y = t; }


SWAP out Existing Apple6s to Apple 7s plus?

YES


How To find reverse of string without using strrev function with code?

with a loop. len= strlen (s); p= s; q= s+len-1; while (p<q) { (swap) ++p; --q; }


What are the conditions to swap in c plus plus without temporary?

You can swap two integers without temporary storage by bitwise exclusive-or'ing them in a specific sequence...a ^= b;b ^= a;a ^= b;


How do you use string in bubble sort in c plus plus code?

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.