-
January 8th, 2013, 03:17 AM
#1
how swap two strings without using 3rd variable? could it be done using constructors?
how swap two strings without using 3rd variable? could it be done using constructors?
if yes how?
-
January 8th, 2013, 03:25 AM
#2
Re: how swap two strings without using 3rd variable? could it be done using construct
If you are dealing with C-style null terminated strings, and have a pointer to the first character of each string, then you could just swap the pointers. If you are dealing with std::string objects, then just use std::swap as the underlying swap is likely to swap pointers rather than perform a full copy with a third string.
-
January 8th, 2013, 08:27 AM
#3
Re: how swap two strings without using 3rd variable? could it be done using construct
Originally Posted by vickytambule
how swap two strings without using 3rd variable? could it be done using constructors?
if yes how?
What type of strings are you talking about?
-
January 8th, 2013, 08:36 AM
#4
Re: how swap two strings without using 3rd variable? could it be done using construct
string for example : "qwerty"
-
January 8th, 2013, 09:28 AM
#5
Re: how swap two strings without using 3rd variable? could it be done using construct
Originally Posted by vickytambule
string for example : "qwerty"
I meant null terminated char arrays, std::string, what?
-
January 8th, 2013, 11:49 AM
#6
Re: how swap two strings without using 3rd variable? could it be done using construct
Originally Posted by vickytambule
string for example : "qwerty"
Code:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string s1 = "abc";
string s2 = "123";
cout << s1 << " " << s2 << "\n";
swap(s1, s2);
cout << s1 << " " << s2 << "\n";
}
Run that example. Is this what you're asking?
Regards,
Paul McKenzie
-
January 8th, 2013, 12:04 PM
#7
Re: how swap two strings without using 3rd variable? could it be done using construct
as a general rule... NO.
Swapping in C/C++ always requires a temporary.
std::swap() just works around the issue, it still uses a temporary, you just didn't define it yourself.
You might be able to pull some "weird" binary or mathematical trick to swap SOME types of variables, but chances are this'll be less efficient in the end than using a temporary for the swap.
-
January 8th, 2013, 12:40 PM
#8
Re: how swap two strings without using 3rd variable? could it be done using construct
That's why I was asking what type of string. You could possibly do it with some combination of concatenating, copying and shifting.
-
January 8th, 2013, 03:05 PM
#9
Re: how swap two strings without using 3rd variable? could it be done using construct
The only situation where it is possible to swap standard null terminated strings without using a 3rd variable is when they are exactly the same size (or the memory allocated for the smaller string is at least that of the larger string). See example below. This also assumes that the max ASCII value of any individual char in the string is 127 otherwise *p1 += *p2 will overflow the bounds of a char.
Code:
#include <iostream>
using namespace std;
int main ()
{
char s1[10] = "abcdef";
char s2[10] = "ghijkl";
cout << "before swap" << endl << s1 << endl << s2 << endl;
for (char *p1 = s1, *p2 = s2; *p1; p1++, p2++) {
*p1 += *p2;
*p2 = *p1 - *p2;
*p1 -= *p2;
}
cout << endl << "after swap" << endl << s1 << endl << s2 << endl;
return (0);
}
-
January 8th, 2013, 03:15 PM
#10
Re: how swap two strings without using 3rd variable? could it be done using construct
Originally Posted by 2kaud
The only situation where it is possible to swap standard null terminated strings without using a 3rd variable is when they are exactly the same size (or the memory allocated for the smaller string is at least that of the larger string). See example below. This also assumes that the max ASCII value of any individual char in the string is 127 otherwise *p1 += *p2 will overflow the bounds of a char.
Generally we don't post solutions to homework here.
-
January 8th, 2013, 07:37 PM
#11
Re: how swap two strings without using 3rd variable? could it be done using construct
Perhaps it doesn't even need to be as nifty as 2kaud's proposal, in case that no temporary string variable (of whatever concrete type) is allowed, while a temporary char variable is...
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
January 9th, 2013, 02:45 AM
#12
Re: how swap two strings without using 3rd variable? could it be done using construct
Originally Posted by 2kaud
This also assumes that the max ASCII value of any individual char in the string is 127
The XOR swap (without a temp) doesn't have this limitation,
http://en.wikipedia.org/wiki/XOR_swap_algorithm
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|