how swap two strings without using 3rd variable? could it be done using constructors?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: how swap two strings without using 3rd variable? could it be done using constructors?

  1. #1
    Join Date
    Jan 2013
    Posts
    2

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,196

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: how swap two strings without using 3rd variable? could it be done using construct

    Quote Originally Posted by vickytambule View Post
    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?

  4. #4
    Join Date
    Jan 2013
    Posts
    2

    Re: how swap two strings without using 3rd variable? could it be done using construct

    string for example : "qwerty"

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: how swap two strings without using 3rd variable? could it be done using construct

    Quote Originally Posted by vickytambule View Post
    string for example : "qwerty"
    I meant null terminated char arrays, std::string, what?

  6. #6
    Join Date
    Apr 1999
    Posts
    27,418

    Re: how swap two strings without using 3rd variable? could it be done using construct

    Quote Originally Posted by vickytambule View Post
    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

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,641

    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.

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    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.

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,997

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

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: how swap two strings without using 3rd variable? could it be done using construct

    Quote Originally Posted by 2kaud View Post
    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.

  11. #11
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,571

    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.

  12. #12
    Join Date
    May 2009
    Posts
    2,413

    Re: how swap two strings without using 3rd variable? could it be done using construct

    Quote Originally Posted by 2kaud View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center