Results 1 to 6 of 6

Thread: change the positions of characters in a string

  1. #1
    Join Date
    Jun 2006
    Location
    San Diego, USA
    Posts
    95
    Thanks
    9
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default change the positions of characters in a string

    Hi

    I want to change the positions of characters in a string.
    Ex: QString x = "341256";
    I want to Change the x string from "341256" to "123456";

    How can I change the character positions within a string.

    Thanks & Regards,
    Arun.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: change the positions of characters in a string

    You mean you want to sort it or make an arbitrary change?

    This might just work:
    Qt Code:
    1. QString str = "341256";
    2. QChar *dat = str.data();
    3. qSort(dat, dat+str.length());
    To copy to clipboard, switch view to plain text mode 

    If you want arbitrary changes then swap characters manually.
    Qt Code:
    1. QString str="342516";
    2. qSwap(str[2], str[3]);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2006
    Location
    San Diego, USA
    Posts
    95
    Thanks
    9
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: change the positions of characters in a string

    Thanks for your reply.
    I gave me error if I am trying to swap characters using qswap. It seems to me qswap is used to swap a whole string, not to swap individual characters inside the string.

    So I resolved the issue by converting QString to Std C++ string and using the method which you stated in reply i.e. swapping.

    Resolved as Shown here:
    Qt Code:
    1. classname::function()
    2. {
    3. ....
    4. QString str = "341256";
    5. char * str1;
    6. str1 = (char *)Q_date.toAscii().constData();
    7. swap( str1[0], str1[2] );
    8. swap( str1[1], str1[3] );
    9. str = (const char *)str1; // Now str has string "123456"
    10.  
    11. ....
    12. }
    13. void clasname::swap(char &a,char& b )
    14. {
    15. char temp = b;
    16. b = a;
    17. a = temp;
    18. }
    To copy to clipboard, switch view to plain text mode 


    Thanks again for giving me idea of swapping.
    Last edited by jpn; 10th January 2008 at 07:19. Reason: missing [code] tags

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: change the positions of characters in a string

    Even if qSwap() doesn't work as expected, it compiles fine for me at least:
    Qt Code:
    1. QString str("12");
    2. qSwap(str[0], str[1]);
    3. qDebug() << str; // "22"
    To copy to clipboard, switch view to plain text mode 

    However, the first method suggested by Wysota works perfectly.
    Qt Code:
    1. QString str("341256");
    2. QChar* dat = str.data();
    3. qSort(dat, dat + str.length());
    4. qDebug() << str; // "12345"
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: change the positions of characters in a string

    Quote Originally Posted by jpn View Post
    However, the first method suggested by Wysota works perfectly.
    So this will work as well:
    Qt Code:
    1. QString str="173562";
    2. QChar *dat = str.data();
    3. qSwap(dat[1], dat[2]);
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: change the positions of characters in a string

    The following code will in most likely cause your application to crash:
    Qt Code:
    1. char * str1 = Q_date.toAscii().constData();
    2. swap( str1[0], str1[2] );
    To copy to clipboard, switch view to plain text mode 
    and even if it doesn't it won't work.
    You shouldn't save a pointer to the temporary object returned by toAscii(), it goes out of scope immediately. Go with Wysota's suggestion.

    @jpn + wysota: It's an interesting side effect of Qt's trick with QCharRef that swap doesn't work as expected. I think they should mention QCharRef in the documentation and that it's sometimes more efficient(in this case necessary) to work on the raw QChar array returned by data()

Similar Threads

  1. Problem at time compilation in traslation of language
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2007, 14:18
  2. inserting string > 127 characters to MS Access
    By jh in forum Qt Programming
    Replies: 0
    Last Post: 12th May 2006, 17:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.