use_str.cpp
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 8k
Development Platform:

C++ Builder

  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <conio.h>
  5. #include <stdio.h>
  6. class Strings {
  7.    char *p;
  8.    int size;
  9.  public:
  10.    Strings(char *str);
  11.    Strings(void);
  12.    Strings(const Strings &obj);           // Copy constructor
  13.    ~Strings(void) {delete [] p;}
  14.    friend ostream &operator<<(ostream &stream, Strings &obj);
  15.    friend istream &operator>>(istream &stream, Strings &obj);
  16.    Strings operator=(Strings &obj);       // assign a Strings object
  17.    Strings operator=(char *s);            // assign a quoted string
  18.    Strings operator+(Strings &obj);       // concatenate a Strings object
  19.    Strings operator+(char *s);            // concatenate a quoted string
  20.    friend Strings operator+(char *s, Strings &obj);
  21.             /* concatenates a quoted string with a Strings object */
  22.    Strings operator-(Strings &obj);       // subtract a Strings object
  23.    Strings operator-(char *s);            // subtract a quoted string
  24.  /* relational operators between Strings objects. Note that the operators could
  25.     just as easily return bool, rather than int */
  26.    int operator==(Strings &obj) {return !strcmp(p, obj.p);}
  27.    int operator!=(Strings &obj) {return strcmp(p, obj.p);}
  28.    int operator<(Strings &obj) {return strcmp(p, obj.p) < 0;}
  29.    int operator>(Strings &obj) {return strcmp(p, obj.p) > 0;}
  30.    int operator<=(Strings &obj) {return strcmp(p, obj.p) <= 0;}
  31.    int operator>=(Strings &obj) {return strcmp(p, obj.p) >= 0;}
  32.  /* relational operators between Strings object and a quoted character string.
  33.     Note that the operators could just as easily return bool, rather than int */
  34.    int operator==(char *s) {return !strcmp(p, s);}
  35.    int operator!=(char *s) {return strcmp(p, s);}
  36.    int operator<(char *s) {return strcmp(p, s) < 0;}
  37.    int operator>(char *s) {return strcmp(p, s) > 0;}
  38.    int operator<=(char *s) {return strcmp(p, s) <= 0;}
  39.    int operator>=(char *s) {return strcmp(p, s) >= 0;}
  40.    int strsize(void) {return strlen(p);}      // return string size
  41.    void makestr(char *s) {strcpy(s, p);}  // make quoted string from Strings object
  42.    operator char *(void) {return p;}          // conversion to char
  43.  };
  44. Strings::Strings(void)
  45.  {
  46.    size = 1;
  47.    p = new char[size];
  48.    if(!p)
  49.     {
  50.       cout << "Allocation error!" << endl;
  51.       exit(1);
  52.     }
  53.    *p = '';
  54.  }
  55. Strings::Strings(char *str)
  56.  {
  57.    size = strlen(str) + 1;
  58.    p = new char[size];
  59.    if(!p)
  60.     {
  61.       cout << "Allocation error!" << endl;
  62.       exit(1);
  63.     }
  64.    strcpy(p, str);
  65.  }
  66. Strings::Strings(const Strings &obj)
  67.  {
  68.    size = obj.size;
  69.    p = new char[size];
  70.    if(!p)
  71.     {
  72.       cout << "Allocation error!" << endl;
  73.       exit(1);
  74.     }
  75.    strcpy(p, obj.p);
  76.  }
  77. ostream &operator<<(ostream &stream, Strings &obj)
  78.  {
  79.    stream << obj.p;
  80.    return stream;
  81.  }
  82. istream &operator>>(istream &stream, Strings &obj)
  83.  {
  84.    char t[255];      // arbitrary string length--yours can be larger
  85.    int len;
  86.    for(len=0; len<255; len++)
  87.     {
  88.       stream.get(t[len]);
  89.       if(t[len]=='n')
  90.          break;
  91.       if(t[len]=='b')
  92.          if(len)
  93.           {
  94.             len--;
  95.             cout << "'b'";
  96.           }
  97.     }
  98.    t[len]='';
  99.    len++;
  100.    if(len>obj.size)
  101.     {
  102.       delete obj.p;
  103.       obj.p = new char[len];
  104.       if(!obj.p)
  105.        {
  106.          cout << "Allocation error!" << endl;
  107.          exit(1);
  108.        }
  109.       obj.size = len;
  110.     }
  111.    strcpy(obj.p, t);
  112.    return stream;
  113.  }
  114. Strings Strings::operator=(Strings &obj)
  115.   {
  116.    Strings temp(obj.p);
  117.    if(obj.size > size)
  118.     {
  119.       delete p;
  120.       p = new char[obj.size];
  121.       size = obj.size;
  122.       if(!p)
  123.        {
  124.          cout << "Allocation error!" << endl;
  125.          exit(1);
  126.        }
  127.     }
  128.    strcpy(p, obj.p);
  129.    strcpy(temp.p, obj.p);
  130.    return temp;
  131.  }
  132. Strings Strings::operator=(char *s)
  133.   {
  134.    int len = strlen(s) + 1;
  135.    if(size < len)
  136.     {
  137.       delete p;
  138.       p = new char[len];
  139.       size = len;
  140.       if(!p)
  141.        {
  142.          cout << "Allocation error!" << endl;
  143.          exit(1);
  144.        }
  145.     }
  146.    strcpy(p, s);
  147.    return *this;
  148.  }
  149. Strings Strings::operator+(Strings &obj)
  150.  {
  151.    int len;
  152.    Strings temp;
  153.    delete temp.p;
  154.    len = strlen(obj.p) + strlen(p) + 1;
  155.    temp.p = new char[len];
  156.    temp.size = len;
  157.    if(!temp.p)
  158.     {
  159.       cout << "Allocation error!" << endl;
  160.       exit(1);
  161.     }
  162.    strcpy(temp.p, p);
  163.    strcat(temp.p, obj.p);
  164.    return temp;
  165.  }
  166. Strings Strings::operator+(char *s)
  167.  {
  168.    int len;
  169.    Strings temp;
  170.    delete temp.p;
  171.    len = strlen(s) + strlen(p) + 1;
  172.    temp.p = new char[len];
  173.    temp.size = len;
  174.    if(!temp.p)
  175.     {
  176.       cout << "Allocation error!" << endl;
  177.       exit(1);
  178.     }
  179.    strcpy(temp.p, p);
  180.    strcat(temp.p, s);
  181.    return temp;
  182.  }
  183. Strings operator+(char *s, Strings &obj)
  184.  {
  185.    int len;
  186.    Strings temp;
  187.    delete temp.p;
  188.    len = strlen(s) + strlen(obj.p) + 1;
  189.    temp.p = new char[len];
  190.    temp.size = len;
  191.    if(!temp.p)
  192.     {
  193.       cout << "Allocation error!" << endl;
  194.       exit(1);
  195.     }
  196.    strcpy(temp.p, s);
  197.    strcat(temp.p, obj.p);
  198.    return temp;
  199.  }
  200. Strings Strings::operator-(Strings &substr)
  201.  {
  202.    Strings temp(p);
  203.    char *s1;
  204.    int i,j;
  205.    s1 = p;
  206.    for(i=0; *s1; i++)
  207.     {
  208.       if(*s1!=*substr.p)
  209.        {
  210.          temp.p[i] = *s1;
  211.          s1++;
  212.        }
  213.       else
  214.        {
  215.          for(j=0; substr.p[j]==s1[j] && substr.p[j]; j++)
  216.             ;
  217.          if(!substr.p[j])
  218.           {
  219.             s1 += j;
  220.             i--;
  221.           }
  222.          else
  223.           {
  224.             temp.p[i] = *s1;
  225.             s1++;
  226.           }
  227.        }
  228.     }
  229.     temp.p[i] = '';
  230.     return temp;
  231.  }
  232. Strings Strings::operator-(char *substr)
  233.  {
  234.    Strings temp(p);
  235.    char *s1;
  236.    int i,j;
  237.    s1 = p;
  238.    for(i=0; *s1; i++)
  239.     {
  240.       if(*s1!=*substr)
  241.        {
  242.          temp.p[i] = *s1;
  243.          s1++;
  244.        }
  245.       else
  246.        {
  247.          for(j=0; substr[j]==s1[j] && substr[j]; j++)
  248.             ;
  249.          if(!substr[j])
  250.           {
  251.             s1 += j;
  252.             i--;
  253.           }
  254.          else
  255.           {
  256.             temp.p[i] = *s1;
  257.             s1++;
  258.           }
  259.        }
  260.     }
  261.     temp.p[i] = '';
  262.     return temp;
  263.  }
  264.  void main(void)
  265.   {
  266.    Strings s1("A sample program which uses string objects.n");
  267.    Strings s2(s1);
  268.    Strings s3;
  269.    char s[80];
  270.    cout << s1 << s2;
  271.    s3 = s1;
  272.    cout << s3;
  273.    s3.makestr(s);
  274.    cout << "Converted to a string: " << s;
  275.    s2 = "This is a new string.";
  276.    cout << s2 << endl;
  277.    Strings s4("This is a new string, too.");
  278.    s1 = s2 + s4;
  279.    cout << s1 << endl;
  280.    if(s2==s3)
  281.       cout << "Strings are equal." << endl;
  282.    if(s2!=s3)
  283.       cout << "Strings are not equal." << endl;
  284.    if(s1<s4)
  285.       cout << "s1 is less than s4." << endl;
  286.    if(s1>s4)
  287.       cout << "s1 is greater than s4." << endl;
  288.    if(s1<=s4)
  289.       cout << "s1 is less than or equal to s4." << endl;
  290.    if(s1>s4)
  291.       cout << "s1 is greater than or equal to s4." << endl;
  292.    if(s2 > "ABC")
  293.       cout << "s2 is greater than 'ABC'" << endl << endl;
  294.    s1 = "one two three one two threen";
  295.    s2 = "two";
  296.    cout << "Initial string: " << s1;
  297.    cout << "String after subtracting two: ";
  298.    s3 = s1 - s2;
  299.    cout << s3;
  300.    cout << endl;
  301.    s4 = "Jamsa's C/C++ ";
  302.    s3 = s4 + "Programmer's Biblen";
  303.    cout << s3;
  304.    s3 = s3 - "C/C++";
  305.    s3 = "This is " + s3;
  306.    cout << s3;
  307.    cout << "Enter a string: ";
  308.    cin >> s1;
  309.    cout << s1 << endl;
  310.    cout << "s1 is " << s1.strsize() << " characters long." << endl;
  311.    puts(s1);
  312.    s1 = s2 = s3;
  313.    cout << s1 << s2 << s3;
  314.    s1 = s2 = s3 = "Program finished.n";
  315.    cout << s1 << s2 << s3;
  316.  }