You can create another string, let's call it S3, and then search S1 for every charaster of S2 and if the character is found, add it to S3.
C++ example:
#include <iostream>
#include <cstring>
int main()
{
std::string s1, s2, s3;
std::cin >> s1 >> s2;
for (int i = 0; i < s2.length(); ++i)
{
for (int j = 0; j < s1.length(); ++j)
{
if (s1[j] == s2[i])
{
s3 = s3 + s1[j];
}
}
}
std::cout << s3;
return 0;
}