Replace all occurrences of a string with space | C ++ implementation to extract the secret message ; Function to extract the secret message ; Replacing all occurrences of Sub in Str by empty spaces ; Removing unwanted spaces in the start and end of the string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string trim ( const string & s ) { auto start = s . begin ( ) ; while ( start != s . end ( ) && isspace ( * start ) ) start ++ ; auto end = s . end ( ) ; do { end -- ; } while ( distance ( start , end ) > 0 && isspace ( * end ) ) ; return string ( start , end + 1 ) ; } string extractSecretMessage ( string str , string sub ) { size_t pos ; while ( ( pos = str . find ( sub ) ) != string :: npos ) str . replace ( pos , 3 , " ▁ " ) ; str = trim ( str ) ; return str ; } int main ( int argc , char const * argv [ ] ) { string str = " LIELIEILIEAMLIECOOL " ; string sub = " LIE " ; cout << extractSecretMessage ( str , sub ) << endl ; return 0 ; }