home *** CD-ROM | disk | FTP | other *** search
/ Beginning C++ Through Gam…rogramming (2nd Edition) / BCGP2E.ISO / source / chapter03 / string_tester.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-04-11  |  1.2 KB  |  46 lines

  1. // String Tester
  2. // Demonstrates string objects
  3.  
  4. #include <iostream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.     string word1 = "Game";
  12.     string word2("Over");
  13.     string word3(3, '!');
  14.  
  15.     string phrase = word1 + " " + word2 + word3;
  16.     cout << "The phrase is: " << phrase << "\n\n";
  17.  
  18.     cout << "The phrase has " << phrase.size() << " characters in it.\n\n";
  19.  
  20.     cout << "The character at position 0 is: " << phrase[0] << "\n\n";
  21.  
  22.     cout << "Changing the character at position 0.\n";
  23.     phrase[0] = 'L';
  24.     cout << "The phrase is now: " << phrase << "\n\n";
  25.  
  26.     for (int i = 0; i < phrase.size(); ++i)
  27.         cout << "Character at position " << i << " is: " << phrase[i] << endl;
  28.  
  29.     cout << "\nThe sequence 'Over' begins at location " << phrase.find("Over") << endl;
  30.  
  31.     if (phrase.find("eggplant") == string::npos)
  32.         cout << "'eggplant' is not in the phrase.\n\n";
  33.  
  34.     phrase.erase(4, 5);
  35.     cout << "The phrase is now: " << phrase << endl;
  36.     phrase.erase(4);
  37.     cout << "The phrase is now: " << phrase << endl;
  38.     phrase.erase();
  39.     cout << "The phrase is now: " << phrase << endl;
  40.  
  41.     if (phrase.empty())
  42.         cout << "\nThe phrase is no more.\n";
  43.  
  44.  return 0;
  45. }
  46.