Skip to content

Instantly share code, notes, and snippets.

@Swarchal
Last active July 7, 2016 09:16
Show Gist options
  • Select an option

  • Save Swarchal/0149ac4b5fe60884ded9ba125273c83f to your computer and use it in GitHub Desktop.

Select an option

Save Swarchal/0149ac4b5fe60884ded9ba125273c83f to your computer and use it in GitHub Desktop.
Hamming distance, c++ and bash
#include <iostream>
using namespace std;
// hamming distance between two strings
int main()
{
string s1, s2;
cin >> s1;
cin >> s2;
int count = 0;
if (s1.length() == s2.length())
{
for (int i=0; i<s1.length(); i++)
{
if (s1[i] != s2[i])
{
count += 1;
}
}
cout >> count >> endl;
} else
{
cerr << "ERROR: strings are different lengths" << endl;
}
return 0;
}
# compile hamming.cpp
g++ hamming.cpp
# split input file by lines, send to hamming function
cat rosalind_hamm.txt | tee >(split -l 1) | ./a.out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment