home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / STRPP11 / STR_TEST.CPP next >
C/C++ Source or Header  |  1992-03-24  |  8KB  |  225 lines

  1. /* -------------------------------------------------------------------- */
  2. /* str_test.cpp                                */
  3. /*                                    */
  4. /* String++ Demo program                   Last revised 03/23/92    */
  5. /* -------------------------------------------------------------------- */
  6.  
  7. #include <stdio.h>
  8. #include <conio.h>
  9. #include <stdlib.h>
  10. #include "str.h"
  11.  
  12. void pause(void);
  13.  
  14. main()
  15. {
  16.   string *array;
  17.   int i, pos, num;
  18.  
  19.   clrscr();
  20.  
  21.   string title1 = "String++ Version 1.1";
  22.   string title2 = "Written by Carl W. Moreland";
  23.   string title3 = "Demonstration of methods & operators";
  24.   string title4 = "Hit any key to continue,";
  25.   string title5 = "or <Ctrl>C to exit...";
  26.   printf("\n\n\n\n\n\n\n\n");
  27.   printf("%s\n",   (justify(title1, CENTER_JUSTIFY, 78))());
  28.   printf("%s\n\n", (justify(title2, CENTER_JUSTIFY, 78))());
  29.   printf("%s\n\n", (justify(title3, CENTER_JUSTIFY, 78))());
  30.   printf("%s\n",   (justify(title4, CENTER_JUSTIFY, 78))());
  31.   printf("%s\n",   (justify(title5, CENTER_JUSTIFY, 78))());
  32.  
  33.   pause();
  34.  
  35.   printf("First, create a string and assign it \"Hello World.\"\n\n");
  36.   string str1 = "Hello World.";
  37.   printf("string str1 = \"Hello World.\";\n\n");
  38.  
  39.   printf("The string contents are returned by the () operator:\n\n");
  40.   printf("str1() = %s\n\n", str1());
  41.  
  42.   printf("The length of str1 is given by the len() member function:\n\n");
  43.   printf("str1.len() = %d\n\n", str1.len());
  44.  
  45.   printf("Placing a number in the () operator returns the substring\n");
  46.   printf("  of the string starting at that number:\n\n");
  47.   printf("str1(6) = %s\n\n", str1(6));
  48.  
  49.   printf("The nth character is returned by the [] operator:\n\n");
  50.   printf("str1[6] = %c\n\n", str1[6]);
  51.  
  52.   pause();
  53.  
  54.   printf("Let's create a new string that's equal to str1*2:\n\n");
  55.   string str2 = str1*2;
  56.   printf("string str2 = str1*2;\n");
  57.   printf("str2() = %s\n\n", str2());
  58.  
  59.   printf("Now multiply str2 by 2:\n\n");
  60.   str2 *= 2;
  61.   printf("str2 *= 2;\n");
  62.   printf("str2() = %s\n\n", str2());
  63.  
  64.   pause();
  65.  
  66.   printf("Create a new string with the contents \"only\":\n\n");
  67.   string str3 = "only";
  68.   printf("string str3 = \"only\";\n\n");
  69.  
  70.   printf("Now use the + operator to add to it:\n\n");
  71.   str3 = "This is " + str3 + " a test";
  72.   printf("str3 = \"This is \" + str3 + \" a test\";\n");
  73.   printf("str3() = %s\n\n", str3());
  74.  
  75.   pause();
  76.  
  77.   printf("Create a new string and use the += operator to append to it:\n\n");
  78.   string str4 = "Please ";
  79.   str4 += "stand by...";
  80.   printf("string str4 = \"Please \";\n");
  81.   printf("str4 += \"stand by...\";\n");
  82.   printf("str4() = %s\n\n", str4());
  83.  
  84.   printf("Use the left(), mid(), & right() functions to return substrings:\n\n");
  85.   printf(" left(str4, 6)    = %s\n", (left(str4, 6))());
  86.   printf("  mid(str4, 7, 5) = %s\n", (mid(str4, 7, 5))());
  87.   printf("right(str4, 5)    = %s\n\n", (right(str4, 5))());
  88.  
  89.   printf("We could also use the member functions to do the same:\n\n");
  90.   printf("  str4.left(6)    = %s\n", (str4.left(6))());
  91.   printf("   str4.mid(7, 5) = %s\n", (str4.mid(7, 5))());
  92.   printf(" str4.right(5)    = %s\n\n", (str4.right(5))());
  93.  
  94.   pause();
  95.  
  96.   printf("Create multiple characters by passing an optional multiplier to\n");
  97.   printf("  the string constructor:\n\n");
  98.   string str5 = "/* " + string('-', 40) + " */";
  99.   printf("string str5 = \"/* \" + string(\'-\', 40) + \" */\";\n");
  100.   printf("str5() = %s\n", str5());
  101.  
  102.   pause();
  103.  
  104.   printf("The justify function will expand a string to a total width of n\n");
  105.   printf("  by adding blanks and justify the non-blank portion:\n\n");
  106.   string str6 = "Hello World.";
  107.   string str6a = "│" + str6.justify(LEFT_JUSTIFY, 20)   + "│";
  108.   string str6b = "│" + str6.justify(CENTER_JUSTIFY, 20) + "│";
  109.   string str6c = "│" + str6.justify(RIGHT_JUSTIFY, 20)  + "│";
  110.  
  111.   printf("string str6 = str1;\n");
  112.   printf("str6a = \"│\" + str6.justify(LEFT_JUSTIFY, 20)   + \"│\";\n");
  113.   printf("str6b = \"│\" + str6.justify(CENTER_JUSTIFY, 20) + \"│\";\n");
  114.   printf("str6c = \"│\" + str6.justify(RIGHT_JUSTIFY, 20)  + \"│\";\n");
  115.   printf("str6a() = %s\n", str6a());
  116.   printf("str6b() = %s\n", str6b());
  117.   printf("str6c() = %s\n", str6c());
  118.  
  119.   printf("\nHere's what happens when clipping is used:\n\n");
  120.   str6a = "│" + str6.justify(LEFT_JUSTIFY, 8, CLIP)   + "│";
  121.   str6b = "│" + str6.justify(CENTER_JUSTIFY, 8, CLIP) + "│";
  122.   str6c = "│" + str6.justify(RIGHT_JUSTIFY, 8, CLIP)  + "│";
  123.  
  124.   printf("str6a = \"│\" + str6.justify(LEFT_JUSTIFY, 8, CLIP)   + \"│\";\n");
  125.   printf("str6b = \"│\" + str6.justify(CENTER_JUSTIFY, 8, CLIP) + \"│\";\n");
  126.   printf("str6c = \"│\" + str6.justify(RIGHT_JUSTIFY, 8, CLIP)  + \"│\";\n");
  127.   printf("str6a() = %s\n", str6a());
  128.   printf("str6b() = %s\n", str6b());
  129.   printf("str6c() = %s\n", str6c());
  130.  
  131.   pause();
  132.  
  133.   printf("The == operator will work for string == string, string == char*,\n");
  134.   printf("  or for char* == string:\n\n");
  135.   if(str1 == "Hello World.")
  136.     printf("str1 == \"Hello World.\"\n");
  137.   else
  138.     printf("str1 != \"Hello World.\"\n");
  139.  
  140.   if("Hello World." == str1)
  141.     printf("\"Hello World.\" == str1\n");
  142.   else
  143.     printf("\"Hello World.\" != str1\n");
  144.  
  145.   pause();
  146.  
  147.   printf("The C-style function toupper() will return the upper case version\n");
  148.   printf("  of a string without changing the string itself, whereas the\n");
  149.   printf("  member function toupper() will convert the string internally:\n\n");
  150.   printf("str1() = %s\n", str1());
  151.   printf("toupper(str1) = %s\n", (toupper(str1))());
  152.   printf("str1() = %s\n", str1());
  153.   str1.toupper();
  154.   printf("str1.toupper();\n");
  155.   printf("str1() = %s\n", str1());
  156.  
  157.   pause();
  158.  
  159.   printf("The following are AWK functions.\n\n");
  160.  
  161.   printf("index() returns the location of a substring in a string:\n\n");
  162.   pos = index(str1, "WOR");
  163.   printf("str1() = %s\n", str1());
  164.   printf("pos = index(str1, \"WOR\");\n");
  165.   printf("pos = %d\n", pos);
  166.  
  167.   pause();
  168.  
  169.   printf("split() will split a string at a given substring delimiter:\n\n");
  170.   string str7 = "d:\\prog\\turboc\\str";
  171.   num = split(str7, &array, "\\");
  172.  
  173.   printf("string str7 = \"d:\\prog\\turboc\\str\";\n");
  174.   printf("num = split(str7, array, \"\\\");\n\n");
  175.  
  176.   printf("In this example, str7 is split using the \\ character as the\n");
  177.   printf("  delimiter. The results are placed in array, which now contains:\n\n");
  178.   for(i=0; i<num; i++)
  179.     printf("array[%d] = %s\n", i, array[i]());
  180.   printf("\n");
  181.  
  182.   pause();
  183.  
  184.   printf("gsub() performs a global substitution. In this example, we want to\n");
  185.   printf("  replace all \\'s with /'s:\n\n");
  186.   printf("str7() = %s\n", str7());
  187.   i = gsub("\\", "/", str7);
  188.   printf("i = gsub(\"\\\", \"/\", str7);\n");
  189.   printf("str7() = %s\ni=%d\n", str7(), i);
  190.  
  191.   pause();
  192.  
  193.   printf("sub() performs a one-time substitution:\n\n");
  194.   i = sub("LO", "P ME,", str1);
  195.   printf("str1() = %s\n", str1());
  196.   printf("i = sub(\"LO\", \"P ME,\", str1);\n");
  197.   printf("str1() = %s\ni = %d\n", str1(), i);
  198.  
  199.   pause();
  200.  
  201.   printf("substr() returns a substring of the string starting at n. If a\n");
  202.   printf("  a third parameter is given, it represents the maximum number\n");
  203.   printf("  of characters to return\n\n");
  204.   string str_3 = substr(str3, 8);
  205.   string str_4 = substr(str4, 7, 5);
  206.   printf("str3() = %s\n", str3());
  207.   printf("substr(str3, 8) = %s\n\n", str_3());
  208.   printf("str4() = %s\n", str4());
  209.   printf("substr(str4, 7, 5) = %s\n", str_4());
  210.  
  211.   pause();
  212.   return 0;
  213. }
  214.  
  215.  
  216. void pause(void)
  217. {
  218.   char ch = getch();
  219.   if(!ch)
  220.     getch();
  221.   if(ch == 0x03)
  222.     exit(0);
  223.   clrscr();
  224.   printf("\n");
  225. }