I think this will work too :
Code: C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <set>
#include <string>

using namespace std;

int main()
{
    string a = "abcdk57";
    string b = "abcdek5";
    string c;

    set_intersection(
        a.begin(), a.end(),
        b.begin(), b.end(),
        back_inserter(c));

    cout << c << '\n';
}