home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch12 / string1.cpp < prev    next >
C/C++ Source or Header  |  2005-06-16  |  2KB  |  50 lines

  1. //             string1.cpp
  2. //
  3. // Synopsis  - Instantiates two string objects. The string
  4. //             objects invoke member functions, and the
  5. //             results are displayed.
  6. //
  7. // Objective - To introduce functions that can be used
  8. //             with strings.
  9. //
  10.  
  11. // Include Files
  12. #include <string>                                    // Note 1
  13. #include <iostream.h>    
  14. using namespace std ;                                // Note 2
  15.  
  16. int main()
  17. {
  18.     string strng1("abc");                            // Note 3
  19.     string strng2("def");
  20.  
  21.     cout << "strng1 = " << strng1.c_str() << endl;   // Note 4
  22.  
  23.     strng1.append(strng2);                           // Note 5
  24.     cout << "After appending strng2, strng1 = " 
  25.          << strng1.c_str() << endl;
  26.  
  27.     strng1.append(strng2, 1, 2);                     // Note 6
  28.     cout << "After appending the last two characters "
  29.          << "of strng2,\nstrng1 = " << strng1.c_str() 
  30.          << endl << endl;
  31.  
  32.     cout << "The character in strng1 at position 2 is " 
  33.          << strng1.at( 2 ) << endl << endl;          // Note 7
  34.     cout << "Size of the strings:" << endl;
  35.     cout << "strng1's size is " 
  36.          << strng1.size() << endl;                   // Note 8
  37.     cout << "strng2's size is " 
  38.          << strng2.length() << endl << endl;         // Note 8
  39.  
  40.     strng2.assign( strng1, 2, 4 );                   // Note 9
  41.     cout << "After assigning four characters from strng1\n"
  42.          << "(starting at position 2) to strng2, strng2 = " 
  43.          << strng2.c_str() << endl << endl;
  44.     
  45.     cout << "Comparing strng1 to strng2: "
  46.          << strng1.compare( strng2 ) << endl;        // Note 10
  47.  
  48.     return 0;
  49. }
  50.