home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 7.ddi / MWHC.007 / 5 < prev    next >
Encoding:
Text File  |  1992-04-01  |  2.9 KB  |  107 lines

  1. /*
  2.  * Example 2: Using the RWCString class
  3.  *
  4.  * $Header:   E:/vcs/toolexam/example2.cpv   1.1   01 Apr 1992 16:51:10   keffer  $
  5.  *
  6.  ****************************************************************************
  7.  *
  8.  * Rogue Wave Software, Inc.
  9.  * P.O. Box 2328
  10.  * Corvallis, OR 97339
  11.  * Voice: (503) 754-2311    FAX: (503) 757-7350
  12.  *
  13.  * Copyright (C) 1989, 1990, 1991. This software is subject to copyright 
  14.  * protection under the laws of the United States and other countries.
  15.  *
  16.  ***************************************************************************
  17.  *
  18.  * $Log:   E:/vcs/toolexam/example2.cpv  $
  19.  * 
  20.  *    Rev 1.1   01 Apr 1992 16:51:10   keffer
  21.  * Now includes <rw/xxx.h>
  22.  * 
  23.  */
  24.  
  25.  
  26. // Include the header file for the class RWCString:
  27. #include <rw/cstring.h>
  28. #include <rw/rstream.h>
  29.  
  30. void exerciseSubString();    // Defined below
  31.  
  32. main()
  33. {
  34.   // Construct an empty string:
  35.   RWCString  x;
  36.  
  37.   // Read a word from the terminal:
  38.   cout << "Type a word: ";
  39.   cin >> x;
  40.   cout << "You typed: " << x << NL;
  41.   
  42.   // Print the number of characters:
  43.   cout << "The length of the word is:\t" << x.length() << NL;
  44.  
  45.   // Assignment to a char*: 
  46.   RWCString a;
  47.   a = "test string";
  48.   cout << a << NL;
  49.   
  50.   // Access to elements with bounds checking:
  51.   cout << "a[3]: " << a[3] << NL;
  52.  
  53.   // Construct two RWCStrings:
  54.   RWCString one = "one";
  55.   RWCString two = "two";
  56.  
  57.   // Concatenate them:
  58.   cout << "one + two :\t" << one + two << NL;
  59.   // Append a char* to one:
  60.   one += "plus";
  61.   cout << "one += plus :\t" << one << NL;
  62.  
  63.   // Convert a RWCString to upper case:;
  64.   RWCString Case("Test Of toUpper() And toLower()");
  65.   cout << "Original string:   " << Case << NL;
  66.   cout << "toUpper() version: " << toUpper(Case) <<NL;
  67.   cout << "toLower() version: " << toLower(Case) <<NL;
  68.  
  69.   // Illustration of pattern search:
  70.   RWCString Pattern = "And";
  71.   cout << "Search for pattern using an RWCString containing \"" << Pattern << "\"\n";
  72.   cout << " Index of first match : " 
  73.        << Case.index(Pattern) << "\n\n";
  74.  
  75.   // Find a char* pattern:
  76.   char* charPattern = "to";
  77.   cout << "Search for pattern using a char* containing \"" << charPattern << "\"\n";
  78.   int iMatch = Case.index(charPattern);
  79.   cout << "Index of first match  : " << iMatch << NL;
  80.   cout << "Index of second match : " 
  81.        << Case.index(charPattern, iMatch+1) << NL;
  82.  
  83.   exerciseSubString();
  84.   return 0;
  85. }
  86.  
  87. /*
  88.  * Exercise the RWSubString class.
  89.  */
  90.  
  91. void
  92. exerciseSubString()
  93. {
  94.   // Construct a RWCString and print it out:
  95.   RWCString Y("History is bunk.");
  96.   cout << "\nTest of Substrings.\nOriginal string \"Y\":\n" << Y << NL;
  97.  
  98.   // Now print out a substring of Y, using a conversion to a RWCString:
  99.   cout << "Y(8,2): " << RWCString(Y(8,2)) << NL;
  100.  
  101.   // Here is an example of using a RWSubString as an lvalue:
  102.   // This changes the RWCString Y.
  103.   Y(8,2) = "was";
  104.   cout << "Use a substring as an l-value:\n";
  105.   cout << "Y(8,2) = \"was\":\n" << Y << NL;
  106. }
  107.