Protocol Online logo
Top : Forum Archives: : Bioinformatics and Biostatistics

FOR C++ EXPERTS - need your help (Jan/06/2007 )

Pages: Previous 1 2 3 4 

Like python you can also loop over strings with c++ - this means we can forget the vectors too.

CODE
#include <iostream>
#include <string>

using namespace std;

static void fillSequence(string& tmp);
static void homology(string& s1, string& s2);

int main() {

    string seq1, seq2;
    fillSequence(seq1);
    fillSequence(seq2);    
    homology(seq1, seq2);
    
}

static void homology(string& s1, string& s2) {
    if (s1.length() == s2.length() ) {
        int same = 0;
        for (int i = 0; i < s1.length(); i++) {
            cout << "Comparing:\t" << s1[i] << ":" << s2[i] << endl;
            if (s1[i] == s2[i]) {
                same = same + 1;
            }    
        }
        float pct = ((float)same / (float)s1.length()) * 100;
        cout << "Seq 1 and 2 are " << pct << "% identical\n" << endl;
    } else {
        cout << "\nThe sequences are not the same length!\nGiving up\n\n" << endl;
        exit(1);    
    }    
}

static void fillSequence(string& tmp) {
    cout << "Please enter a sequence:" << endl;
    getline(cin, tmp);
}

-perlmunky-

Pages: Previous 1 2 3 4