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
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.
Start by looking at these components: an integer counter variable, a for() loop, QString::length(), QString:at(), and an if() statement.
arumita (26th May 2014)
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.Start by looking at these components: an integer counter variable, a for() loop, QString::length(), QString:at(), and an if() statement.
arumita (26th May 2014)
thanx ChrisW67;
i tried like this:
it get compiled but no output did i miss something???Qt Code:
#include <QCoreApplication> #include <QString> #include <QTextStream> int main(void) { int n1 = w1.length(); int n2 = w2.length(); int count=0; int i; for(i=0;i<=n1;i++) { if (w1.at(i)==w2.at(i)) count++; } float k = (count/n1)*100; if(k>=66) out<<"there is a matching"<<endl; else out<<"no matching"<< endl; }To copy to clipboard, switch view to plain text mode
thnx
Last edited by anda_skoa; 26th May 2014 at 18:41. Reason: missing [code] tags
You surely have an off-by-one error in your loop. The last index of an N sized string is N-1.
arumita (26th May 2014)
count and n1 are integers. So if count < n1 result of count/n1 == 0 then k == 0.
Change to this :
Qt Code:
float k = ((float)count/n1)*100To copy to clipboard, switch view to plain text mode
arumita (26th May 2014)
What happens when the two strings are not the same length?
Bookmarks