home *** CD-ROM | disk | FTP | other *** search
- /*
- * Example 2: Using the RWCString class
- *
- * $Header: E:/vcs/toolexam/example2.cpv 1.1 01 Apr 1992 16:51:10 keffer $
- *
- ****************************************************************************
- *
- * Rogue Wave Software, Inc.
- * P.O. Box 2328
- * Corvallis, OR 97339
- * Voice: (503) 754-2311 FAX: (503) 757-7350
- *
- * Copyright (C) 1989, 1990, 1991. This software is subject to copyright
- * protection under the laws of the United States and other countries.
- *
- ***************************************************************************
- *
- * $Log: E:/vcs/toolexam/example2.cpv $
- *
- * Rev 1.1 01 Apr 1992 16:51:10 keffer
- * Now includes <rw/xxx.h>
- *
- */
-
-
- // Include the header file for the class RWCString:
- #include <rw/cstring.h>
- #include <rw/rstream.h>
-
- void exerciseSubString(); // Defined below
-
- main()
- {
- // Construct an empty string:
- RWCString x;
-
- // Read a word from the terminal:
- cout << "Type a word: ";
- cin >> x;
- cout << "You typed: " << x << NL;
-
- // Print the number of characters:
- cout << "The length of the word is:\t" << x.length() << NL;
-
- // Assignment to a char*:
- RWCString a;
- a = "test string";
- cout << a << NL;
-
- // Access to elements with bounds checking:
- cout << "a[3]: " << a[3] << NL;
-
- // Construct two RWCStrings:
- RWCString one = "one";
- RWCString two = "two";
-
- // Concatenate them:
- cout << "one + two :\t" << one + two << NL;
- // Append a char* to one:
- one += "plus";
- cout << "one += plus :\t" << one << NL;
-
- // Convert a RWCString to upper case:;
- RWCString Case("Test Of toUpper() And toLower()");
- cout << "Original string: " << Case << NL;
- cout << "toUpper() version: " << toUpper(Case) <<NL;
- cout << "toLower() version: " << toLower(Case) <<NL;
-
- // Illustration of pattern search:
- RWCString Pattern = "And";
- cout << "Search for pattern using an RWCString containing \"" << Pattern << "\"\n";
- cout << " Index of first match : "
- << Case.index(Pattern) << "\n\n";
-
- // Find a char* pattern:
- char* charPattern = "to";
- cout << "Search for pattern using a char* containing \"" << charPattern << "\"\n";
- int iMatch = Case.index(charPattern);
- cout << "Index of first match : " << iMatch << NL;
- cout << "Index of second match : "
- << Case.index(charPattern, iMatch+1) << NL;
-
- exerciseSubString();
- return 0;
- }
-
- /*
- * Exercise the RWSubString class.
- */
-
- void
- exerciseSubString()
- {
- // Construct a RWCString and print it out:
- RWCString Y("History is bunk.");
- cout << "\nTest of Substrings.\nOriginal string \"Y\":\n" << Y << NL;
-
- // Now print out a substring of Y, using a conversion to a RWCString:
- cout << "Y(8,2): " << RWCString(Y(8,2)) << NL;
-
- // Here is an example of using a RWSubString as an lvalue:
- // This changes the RWCString Y.
- Y(8,2) = "was";
- cout << "Use a substring as an l-value:\n";
- cout << "Y(8,2) = \"was\":\n" << Y << NL;
- }
-