Skip to content

Instantly share code, notes, and snippets.

@Swarchal
Created July 30, 2016 10:59
Show Gist options
  • Save Swarchal/74cda8670453374b0e19b731c11e1397 to your computer and use it in GitHub Desktop.
Save Swarchal/74cda8670453374b0e19b731c11e1397 to your computer and use it in GitHub Desktop.
#include <string>
#include <iostream>
#include <cassert>
using namespace std;
string complement(string s)
{
string n;
string out_string;
// loop through s in reverse order
for (int i=s.length(); i>0; i--) {
// get current nucleotide as uppercase
n = toupper(s[i]);
if (n == "A") {
n = "T";
} else if (n == "T") {
n = "A";
} else if (n == "C") {
n = "G";
} else if (n == "G") {
n = "C";
}
out_string += n;
}
cout << "input length = " << s.length() << endl;
cout << "output length = " << out_string.length() << endl;
assert(s.length() == out_string.length());
return out_string;
}
int main()
{
string test_string = "CGTA";
string out = complement(test_string);
cout << "input: " << test_string << endl;
cout << "output: " << out << endl;
return 0;
}
@Swarchal
Copy link
Author

output:

input length = 4
output length = 4
input:  CGTA
output: TAC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment