I would try to use std::mismatch
(documentation)
template <class InputIterator1, class InputIterator2>
pair<InputIterator1, InputIterator2>
mismatch (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2 );
Return first position where two ranges differ
Compares the elements in the range [first1,last1)
against those in the range beginning at first2
sequentially, and returns where the first mismatch happens.
Some code:
string
mismatch_string( string const & a, string const & b ) {
string::const_iterator longBegin, longEnd, shortBegin;
if( a.length() >= b.length() ) {
longBegin = a.begin();
longEnd = a.end();
shortBegin = b.begin();
}
else {
longBegin = b.begin();
longEnd = b.end();
shortBegin = a.begin();
}
pair< string::const_iterator, string::const_iterator > mismatch_pair =
mismatch( longBegin, longEnd, shortBegin );
return string( mismatch_pair.first, longEnd );
}
A full example with output is uploaded at codepad.