StringFind.cc


// Section:     Section 4.3.5 -- Searching for Substrings and Characters
#include <iostream>
#include <string>
#include <assert.h> // needed for Visual C
using namespace std;

int main()
{
    string str( "one hello is like any other hello" );
    string searchString( "hello" );
    string replaceString( "armadillo" );

    assert( searchString != replaceString );

    string::size_type pos = 0;
    while ( (pos = str.find(searchString, pos)) != string::npos ) {  
        str.replace( pos, searchString.size(), replaceString );
        pos += replaceString.size();
    }
    cout << str << endl;   //one armadillo is like any other armadillo
    return 0;
}


Maintained by John Loomis, updated Mon Jan 01 14:06:07 2007