home *** CD-ROM | disk | FTP | other *** search
- /* -------------------------------------------------------------------- */
- /* String++ Version 2.10 demo.cpp Last revised 10/08/92 */
- /* */
- /* Enhanced string class for Turbo C++/Borland C++. */
- /* Copyright 1991, 1992 by Carl W. Moreland */
- /* -------------------------------------------------------------------- */
- /* Demonstration of String++ methods. */
- /* -------------------------------------------------------------------- */
-
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #include "str.h"
-
- void pause(void);
-
- void intro()
- {
- clrscr();
- _setcursortype(_NOCURSOR);
-
- String title1 = "String++ Version 2.10";
- String title2 = "Written by Carl W. Moreland";
- String title3 = "Demonstration of methods & operators";
- String title4 = "Hit any key to continue,";
- String title5 = "or <Ctrl>C to exit...";
-
- cout << "\n\n\n\n\n\n\n\n";
- cout << justify(title1, CENTER_JUSTIFY, 78) << "\n";
- cout << justify(title2, CENTER_JUSTIFY, 78) << "\n\n";
- cout << justify(title3, CENTER_JUSTIFY, 78) << "\n\n";
- cout << justify(title4, CENTER_JUSTIFY, 78) << "\n";
- cout << justify(title5, CENTER_JUSTIFY, 78) << "\n";
-
- pause();
- }
-
- void test1()
- {
- cout << "Create a string and assign it \"Hello World.\":\n";
- string str1 = "Hello World.";
- cout << "string str1 = \"Hello World.\";\n\n";
-
- cout << "The string contents are returned by the () operator:\n";
- cout << "str1() = " << str1 << "\n\n";
-
- cout << "Now assign str1 to a second string:\n";
- string str2 = str1;
- cout << "string str2 = str1;\n";
- cout << "str2() = " << str2 << "\n\n";
-
- cout << "We can also assign numeric values to strings:\n";
- str2 = 1024;
- cout << "str2 = 1024;\n";
- cout << "str2() = │" << str2 << "│\n\n";
-
- cout << "Create multiple characters by passing an optional multiplier to\n";
- cout << " the string constructor:\n\n";
- str2 = "/* " + string('-', 40) + " */";
- cout << "str2 = \"/* \" + string(\'-\', 40) + \" */\";\n";
- cout << "str2() = " << str2() << "\n\n";
-
- pause();
-
- cout << "Placing a number in the () operator returns the substring\n";
- cout << " of the string starting at that number:\n";
- cout << "str1(6) = " << str1(6) << "\n\n";
-
- cout << "Placing a second number in the () operator limits the substring\n";
- cout << " to that many characters:\n";
- cout << "str1(6,2) = " << str1(6,2) << "\n\n";
-
- cout << "The nth character is returned by the [] operator:\n";
- cout << "str1[6] = " << str1[6] << "\n\n";
-
- cout << "The [] operator can also be used to replace the nth character:\n";
- str1[6] = 'w';
- cout << "str1[6] = 'w';\n";
- cout << "str1() = " << str1 << "\n\n";
-
- cout << "The length of str1 is given by the Length() member function:\n";
- cout << "str1.Length() = " << str1.Length() << "\n\n";
-
- pause();
-
- cout << "The == operator will work for string == string, string == char*,\n";
- cout << " or for char* == string:\n\n";
- if(str1 == "Hello world.")
- cout << "str1 == \"Hello world.\"\n";
- else
- cout << "str1 != \"Hello world.\"\n";
-
- if("Hello world." == str1)
- cout << "\"Hello world.\" == str1\n";
- else
- cout << "\"Hello world.\" != str1\n";
-
- pause();
-
- cout << "Let's create a string that's equal to str1*2:\n\n";
- str2 = str1*2;
- cout << "str2 = str1*2;\n";
- cout << "str2() = " << str2() << "\n\n";
-
- cout << "Now multiply str2 by 2:\n\n";
- str2 *= 2;
- cout << "str2 *= 2;\n";
- cout << "str2() = " << str2() << "\n\n";
-
- pause();
-
- cout << "The C-style function toupper() will return the upper case version\n";
- cout << " of a string without changing the string itself, whereas the\n";
- cout << " member function toUpper() will convert the string internally:\n\n";
- cout << "str1() = " << str1 << "\n";
- cout << "toupper(str1) = " << toupper(str1) << "\n";
- cout << "str1() = " << str1 << "\n";
- str1.toUpper();
- cout << "str1.toUpper();\n";
- cout << "str1() = " << str1 << "\n\n";
-
- pause();
- }
-
- void test2()
- {
- cout << "Create a new string with the contents \"only\":\n\n";
- string str3 = "only";
- cout << "string str3 = \"only\";\n\n";
-
- cout << "Now use the + operator to add to it:\n\n";
- str3 = "This is " + str3 + " a test";
- cout << "str3 = \"This is \" + str3 + \" a test\";\n";
- cout << "str3() = " << str3() << "\n\n";
-
- cout << "The Delete() member function can be used to delete a substring:\n\n";
- str3.Delete(8, 5);
- cout << "str3.Delete(8, 5);\n";
- cout << "str3() = " << str3() << "\n\n";
-
- cout << "The Insert() member function can be used to insert a substring:\n\n";
- str3.Insert(8, "still ");
- cout << "str3.Insert(8, \"still \");\n";
- cout << "str3() = " << str3() << "\n\n";
-
- pause();
-
- cout << "Create a new string and use the += operator to append to it:\n\n";
- string str4 = "Please ";
- str4 += "stand by...";
- cout << "string str4 = \"Please \";\n";
- cout << "str4 += \"stand by...\";\n";
- cout << "str4() = " << str4() << "\n\n";
-
- cout << "Use the left(), mid(), & right() functions to return substrings:\n\n";
- cout << " left(str4, 6) = " << left(str4, 6) << "\n";
- cout << " mid(str4, 7, 5) = " << mid(str4, 7, 5) << "\n";
- cout << "right(str4, 5) = " << right(str4, 5) << "\n\n";
-
- pause();
-
- cout << "The justify() function will expand a string to a total width of n\n";
- cout << " by adding blanks and justify the non-blank portion:\n\n";
- string str5 = "Hello world.";
- string str5a = "│" + justify(str5, LEFT, 20) + "│";
- string str5b = "│" + justify(str5, CENTER, 20) + "│";
- string str5c = "│" + justify(str5, RIGHT, 20) + "│";
-
- cout << "string str5 = str1;\n";
- cout << "str5a = \"│\" + justify(str5, LEFT, 20) + \"│\";\n";
- cout << "str5b = \"│\" + justify(str5, CENTER, 20) + \"│\";\n";
- cout << "str5c = \"│\" + justify(str5, RIGHT, 20) + \"│\";\n";
- cout << "str5a() = " << str5a() << "\n";
- cout << "str5b() = " << str5b() << "\n";
- cout << "str5c() = " << str5c() << "\n";
-
- cout << "\nHere's what happens when clipping is used:\n\n";
- str5a = "│" + justify(str5, LEFT, 8, CLIP) + "│";
- str5b = "│" + justify(str5, CENTER, 8, CLIP) + "│";
- str5c = "│" + justify(str5, RIGHT, 8, CLIP) + "│";
-
- cout << "str5a = \"│\" + justify(str5, LEFT, 8, CLIP) + \"│\";\n";
- cout << "str5b = \"│\" + justify(str5, CENTER, 8, CLIP) + \"│\";\n";
- cout << "str5c = \"│\" + justify(str5, RIGHT, 8, CLIP) + \"│\";\n";
- cout << "str5a() = " << str5a() << "\n";
- cout << "str5b() = " << str5b() << "\n";
- cout << "str5c() = " << str5c() << "\n";
-
- pause();
-
- cout << "The trim() function will remove leading and trailing whitespace:\n\n";
- string str6 = "\t\t " + str5 + " \t ";
- cout << "string str6 = \"\\t\\t \" + str5 + \" \\t \";\n\n";
-
- string str6a = trim(str6, LEFT);
- cout << "str6a = trim(str6, LEFT);\n";
- cout << "str6a() = │" << str6a() << "│\n\n";
- string str6b = trim(str6, RIGHT);
- cout << "str6b = trim(str6, RIGHT);\n";
- cout << "str6b() = │" << str6b() << "│\n\n";
- string str6c = trim(str6);
- cout << "str6c = trim(str6);\n";
- cout << "str6c() = │" << str6c() << "│\n\n";
-
- cout << "You can also specify the character to be trimmed:\n\n";
- str6 = "Here we go again..........";
- cout << "str6 = " << str6 << "\n";
- str6.Trim(RIGHT, '.');
- cout << "str6.Trim(RIGHT, '.');\n";
- cout << "str6 = " << str6 << "\n";
-
- pause();
- }
-
- void test3()
- {
- int i, pos, num;
- string *array;
- string str1 = "HELLO WORLD.";
- string str3 = "This is only a test";
- string str4 = "Please stand by...";
-
- cout << "The following are AWK functions.\n\n";
-
- cout << "index() returns the location of a substring in a string:\n\n";
- pos = index(str1, "WOR");
- cout << "str1() = " << str1 << "\n";
- cout << "pos = index(str1, \"WOR\";\n";
- cout << "pos = " << pos << "\n";
-
- pause();
-
- cout << "split() will split a string at a given substring delimiter:\n\n";
- string str7 = "d:\\prog\\turboc\\str";
- num = split(str7, &array, "\\");
-
- cout << "string str7 = \"d:\\prog\\turboc\\str\";\n";
- cout << "num = split(str7, array, \"\\\";\n\n";
-
- cout << "In this example, str7 is split using the \\ character as the\n";
- cout << " delimiter. The results are placed in array, which now contains:\n\n";
- for(i=0; i<num; i++)
- cout << "array[" << i << "] = " << array[i] << "\n";
-
- pause();
-
- cout << "gsub() performs a global substitution. In this example, we want to\n";
- cout << " replace all \\'s with /'s:\n\n";
- cout << "str7() = " << str7 << "\n";
- i = gsub("\\", "/", str7);
- cout << "i = gsub(\"\\\", \"/\", str7);\n";
- cout << "str7() = " << str7 << "\n";
- cout << "i = " << i << "\n";
-
- pause();
-
- cout << "sub() performs a one-time substitution:\n\n";
- cout << "str1() = " << str1 << "\n";
- i = sub("LO", "P ME,", str1);
- cout << "i = sub(\"LO\", \"P ME,\", str1);\n";
- cout << "str1() = " << str1 << "\n";
- cout << "i = " << i << "\n";
-
- pause();
-
- cout << "substr() returns a substring of the string starting at n. If a\n";
- cout << " a third parameter is given, it represents the maximum number\n";
- cout << " of characters to return\n\n";
- string str_3 = substr(str3, 8);
- string str_4 = substr(str4, 7, 5);
- cout << "str3() = " << str3 << "\n";
- cout << "substr(str3, 8) = " << str_3 << "\n\n";
- cout << "str4() = " << str4 << "\n";
- cout << "substr(str4, 7, 5) = " << str_4 << "\n\n";
-
- pause();
- }
-
- main()
- {
- intro();
- test1();
- test2();
- test3();
- return 0;
- }
-
- void pause(void)
- {
- char ch = getch();
- if(!ch)
- getch();
- if(ch == 0x03)
- exit(0);
- clrscr();
- cout << "\n";
- }
-