Results 1 to 7 of 7

Thread: compare two string variables

  1. #1
    Join Date
    May 2014
    Posts
    8
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default compare two string variables

    hello^^
    plz, how to compare two string variables (character by character) in qt ->to count the number of similar(or matched) characters in the same positions
    thnx
    Last edited by arumita; 25th May 2014 at 19:31.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: compare two string variables

    Start by looking at these components: an integer counter variable, a for() loop, QString::length(), QString:at(), and an if() statement.

  3. The following user says thank you to ChrisW67 for this useful post:

    arumita (26th May 2014)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,972
    Thanks
    277
    Thanked 808 Times in 797 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: compare two string variables

    Start by looking at these components: an integer counter variable, a for() loop, QString::length(), QString:at(), and an if() statement.
    It would probably be helpful to clear some space on your table so you can arrange the components in different ways until you get something that looks like it might solve the problem.

  5. The following user says thank you to d_stranz for this useful post:

    arumita (26th May 2014)

  6. #4
    Join Date
    May 2014
    Posts
    8
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: compare two string variables

    thanx ChrisW67;
    i tried like this:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QString>
    3. #include <QTextStream>
    4.  
    5.  
    6.  
    7. int main(void)
    8. {
    9.  
    10. QTextStream out(stdout);
    11. QString w1 = "amanda";
    12. QString w2 = "amondy";
    13. int n1 = w1.length();
    14. int n2 = w2.length();
    15. int count=0;
    16. int i;
    17. for(i=0;i<=n1;i++)
    18. {
    19. if (w1.at(i)==w2.at(i))
    20. count++;
    21. }
    22.  
    23. float k = (count/n1)*100;
    24. if(k>=66)
    25. out<<"there is a matching"<<endl;
    26. else
    27. out<<"no matching"<< endl;
    28.  
    29.  
    30. }
    To copy to clipboard, switch view to plain text mode 
    it get compiled but no output did i miss something???
    thnx
    Last edited by anda_skoa; 26th May 2014 at 18:41. Reason: missing [code] tags

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

    Default Re: compare two string variables

    You surely have an off-by-one error in your loop. The last index of an N sized string is N-1.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    arumita (26th May 2014)

  9. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,530
    Thanked 281 Times in 276 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: compare two string variables

    count and n1 are integers. So if count < n1 result of count/n1 == 0 then k == 0.
    Change to this :
    Qt Code:
    1. float k = ((float)count/n1)*100
    To copy to clipboard, switch view to plain text mode 

  10. The following user says thank you to Lesiok for this useful post:

    arumita (26th May 2014)

  11. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: compare two string variables

    What happens when the two strings are not the same length?

Similar Threads

  1. Qtime compare
    By valgaba in forum Qt Programming
    Replies: 2
    Last Post: 6th September 2013, 16:46
  2. how to locale compare two string ?
    By narutokage in forum Qt Programming
    Replies: 4
    Last Post: 16th August 2011, 09:13
  3. How do compare the two icons
    By addu in forum Qt Programming
    Replies: 1
    Last Post: 10th July 2009, 16:19
  4. creating query string from variables
    By locus in forum General Programming
    Replies: 2
    Last Post: 16th April 2007, 08:50
  5. Compare
    By merry in forum Qt Programming
    Replies: 1
    Last Post: 14th March 2007, 15:03

Tags for this Thread

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.