Compare two strings

Feb 6, 2017 at 12:10am
closed account (4ybDGNh0)
I am trying to write a code that you input one persons DNA then another Person's DNA and it scores it based on matches. For example if two T's (or A's) match the score is +2, if 2 C's (or G's) match the score is +3 and if there is not a match the score is -2. I am having trouble creating an array that will do different scores for different letters. Here is my code so far. Basically, everything is done except for the "Compare to " function.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Person {
string name;
string dna;

public:
void setName(string s) {
name = s;
}

void inputName(int i) {
cout << "Please enter the name of relative #" << i << ": ";
cin >> name;
}

void inputDNA() {
cout << "Please enter the DNA sequence for "
<< name << ": ";
cin >> dna;

}

void compareTo(Person p) {
int count = 0;

for (size_t i = 0; i < dna.length(); i++)
{
if (p.dna[i] == dna[i])
count++;
}

//THIS IS THE FUNCTION I AM STUCK ON PLS HELP
}
};


int main() {
vector<Person> people;
Person you;
int n;

you.setName("yourself");
you.inputDNA();

cout << "Enter the number of potential relatives :";
cin >> n;

for (int i = 0; i < n; i++)
{
Person p;
p.inputName(i+1);
p.inputDNA();

people.push_back(p);
}


for (int i = 0; i < n; i++)
{
people[i].compareTo(you);
}

return 0;
}
Feb 6, 2017 at 1:09am
pretend we don't know what your dna string looks like.
is it random letters? Sorted? Fixed length?
How you compare them is probably going to be letter by letter, but the actual comparison criteria is unclear.
Feb 6, 2017 at 5:19am
closed account (4ybDGNh0)
You can basically input whatever you want for the DNA as long as the only letters used are A, T, C, and G. For example, sequence 1 and two are my inputs and the rest is what the output should be. I just don't have very much knowledge on arrays or how to go about comparing them

Sequence 1: ATGCTGACTGCA
Sequence 2: CTTGAGACG
A/T score = 3*2= 6
C/G score = 3*3= 9
Non-match = 6*-2= -12
Total score = 3
Feb 6, 2017 at 5:28am
Just a quick note that to compare two strings together, you can use strcmpi() function which compares two strings together.

Ex.

1
2
3
4
5
6
7
8
9
10
int ctr = 0;

// mpass and npass are your strings.
ctr = strcmpi (mpass, npass );
if (ctr == 0 ){
    cout << "correct" << endl;
}
else {
   cout << "wrong" << endl;
Feb 6, 2017 at 5:33am
closed account (4ybDGNh0)
Can i use that to compare individual parts of a string or just two strings as whole?
For example if I compared "Hello" with "Holla" it would just give me an output of -10, rather than giving me being able to give it +2 for every match and -2 for every mismatch if that makes sense (so i would want the output score to be +6)

Feb 6, 2017 at 5:49am
Can i use that to compare individual parts of a string or just two strings as whole?
For example if I compared "Hello" with "Holla" it would just give me an output of -10, rather than giving me being able to give it +2 for every match and -2 for every mismatch if that makes sense (so i would want the output score to be +6)

You'd have to define a function that does that. Simply loop through all the characters in the string and if they match add 2 to your score variable, otherwise subtract 2.
Feb 6, 2017 at 5:53am
closed account (4ybDGNh0)
What type of loop would I use to do that? a for loop?
Feb 6, 2017 at 6:04am
What type of loop would I use to do that? a for loop?

Whichever loop allows you to iterate over each character in a string.
Last edited on Feb 6, 2017 at 6:07am
Topic archived. No new replies allowed.