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

12y 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;


What algorithm use to swap adjacent characters?

To swap adjacent characters in a string, you can use a simple iterative algorithm that processes the string two characters at a time. For each pair of characters, you can swap them and append the result to a new string. This can be done using a loop that increments the index by 2 to ensure adjacent characters are swapped. In Python, for example, you could achieve this with a list comprehension or by converting the string into a list, performing the swaps, and then joining it back into a string.


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 do you reverse a string in emu8086?

To reverse a string in emu8086, you can use a simple loop to swap characters from the start and end of the string until you reach the middle. Load the address of the string, calculate its length, and then use two pointers: one at the beginning and one at the end of the string. Swap the characters at these pointers and move the pointers towards the center, continuing this process until they meet. Finally, the string will be reversed in place.


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


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


SWAP out Existing Apple6s to Apple 7s plus?

YES