home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C17 / Replace.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  879 b   |  31 lines

  1. //: C17:Replace.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. #include <string>
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. void replaceChars(string& modifyMe, 
  11.   string findMe, string newChars){
  12.   // Look in modifyMe for the "find string"
  13.   // starting at position 0
  14.   int i = modifyMe.find(findMe, 0);
  15.   // Did we find the string to replace?
  16.   if(i != string::npos)
  17.     // Replace the find string with newChars
  18.     modifyMe.replace(i,newChars.size(),newChars);
  19. }
  20.  
  21. int main() {
  22.   string bigNews = 
  23.    "I thought I saw Elvis in a UFO. "
  24.    "I have been working too hard.";
  25.   string replacement("wig");
  26.   string findMe("UFO");
  27.   // Find "UFO" in bigNews and overwrite it:
  28.   replaceChars(bigNews, findMe,  replacement);
  29.   cout << bigNews << endl;
  30. } ///:~
  31.