answersLogoWhite

0


Best Answer

The most efficient way to swap strings is to point at them, and swap the pointers. Swapping the actual strings is problematic if the strings are of unequal length but impossible when they are also statically allocated. Dynamic strings can always be physically swapped, however it's highly inefficient. If it really must be done, then use a built-in method such as string::swap(). Otherwise just use string pointers and swap the pointers, never the strings themselves.

The following example shows how both techniques can be used on statically allocated strings. Example output is show below.

#include <iostream>

using namespace std;

void SwapPointers(char ** pp1, char ** pp2 )

{

cout<<"\nSwapping pointers:"<<endl;

char * t = *pp1;

*pp1 = *pp2;

*pp2 = t;

}

void SwapStatics(char * String1, char * String2, size_t len)

{

cout<<"\nSwapping static strings:"<<endl;

while(len--) String1[len]^=String2[len]^=String1[len]^=String2[len];

}

int main()

{

char s1[] = "one ";

char s2[] = "two ";

char s3[] = "three ";

// Note that the output statements before and after

// swapping are exactly the same, proving the strings

// have really swapped.

cout<<"Original strings:"<<endl;

cout<<s1<<s2<<s3<<endl;

SwapStatics(s1,s2,sizeof(s1));

cout<<s1<<s2<<s3<<endl;

cout<<endl;

// We cannot swap s3 with either s1 or s2, because

// s1 and s2 don't have the space to accomodate s3.

// However, we can use pointers instead:

char * p1 = s1;

char * p2 = s2;

char * p3 = s3;

// Again, note that the output statements are the

// same before and after swapping, proving the

// pointers have swapped.

cout<<"Original pointers:"<<endl;

cout<<p1<<p2<<p3<<endl;

SwapPointers(&p1,&p3);

cout<<p1<<p2<<p3<<endl;

cout<<endl;

// Just to prove the strings didn't swap...

cout<<"The strings have not swapped:"<<endl;

cout<<s1<<s2<<s3<<endl;

cout<<endl;

return(0);

}

Output:

Original strings:

one two three

Swapping static strings:

two one three

Original pointers:

two one three

Swapping pointers:

three one two

The strings have not swapped:

two one three

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

//Using string.swap function to swap two strings
#include
#include
#include
using namespace std;
int main()
{
string frst("one");
string scnd("two");
cout<<" Before swap:\n First: "<frst.swap(scnd);
cout<<"\n After swap:\n First: "<getch();
return 0;
}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Except in certain limiting cases, you cannot swap two strings without using temporary variables.

Case one is where the two strings are actually pointers to strings. In that case, you can swap the pointer values, effectively swapping the strings.

Case two is where the two strings are not pointers, andthe strings have the same length. However, you need an index or pointer to walk through the strings, so, if that counts as a temporary variable, then case two is out.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you swap two strings in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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

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


How do you swap two arrays in c plus plus using string?

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.


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;


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

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


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 swap two numbers with bitwise operator in c plus plus?

// Note: ^ is the XOR operator a = a ^ b b = b ^ a a = a ^ b


How do you swap two numbers in c plus plus?

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


Bubble sorting in c plus plus?

bubble_sort (int N, int *A) { int i; swap = 1; while (swap) { swap = 0; for (i=0; i&lt;N-1; ++i) { if (A[i] &gt; A[i+1]) { swap = 1; A[i] ^= A[i+1]; A[i+1] ^= A[i]; A[i] ^= A[i+1]; } } } }


Do you have to use delete operator on C plus plus strings before exiting your application?

no you dont


Is it possibly to return an array of strings in a function without using pointers in C plus plus?

No.


How can you get a specific string from large string using c plus plus strings?

std::string::substr();


What is the use of strcmp function in c plus plus?

strcmp is used to compare two strings. If the return value is zero, the two strings are the same. If the return value is less than 0, then the first string is less than the second string, otherwise the first string is greater than the second string. Strings are compared lexicographically, character by character.