Advertisement
Guest User

String class

a guest
Oct 19th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.45 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include "string"
  4. #include <new>
  5. #include <cstring>
  6. #include <cstdlib>
  7. using namespace std;
  8.  
  9. class StrType {
  10.    
  11.     int size;
  12. public:
  13.     char *p;
  14.     StrType();
  15.     StrType(char *str);
  16.     StrType(const StrType &o); // copy constructor
  17.  
  18.     ~StrType() { delete[] p; }
  19.  
  20.     friend ostream &operator<<(ostream &stream, StrType &o);
  21.     friend istream &operator>>(istream &stream, StrType &o);
  22.  
  23.     StrType operator=(StrType &o); // assign a StrType object
  24.     StrType operator=(char *s);
  25.     StrType operator=(const char *s);// assign a quoted string
  26.  
  27.     StrType operator+(StrType &o); // concatenate a StrType object
  28.     StrType operator+(char *s); // concatenate a quoted string
  29.     friend StrType operator+(char *s, StrType &o); /*  concatenate
  30.                                                    a quoted string with a StrType object */
  31.  
  32.     StrType operator-(StrType &o); // subtract a substring
  33.     StrType operator-(char *s); // subtract a quoted substring
  34.  
  35.     void operator+=(StrType &o);
  36.     void operator+=(char *s);
  37.  
  38.     bool insertstr(StrType &p, StrType &o, int position);
  39.     // relational operations between StrType objects
  40.     int operator==(StrType &o) { return !strcmp(p, o.p); }
  41.     int operator!=(StrType &o) { return strcmp(p, o.p); }
  42.     int operator<(StrType &o) { return strcmp(p, o.p) < 0; }
  43.     int operator>(StrType &o) { return strcmp(p, o.p) > 0; }
  44.     int operator<=(StrType &o) { return strcmp(p, o.p) <= 0; }
  45.     int operator>=(StrType &o) { return strcmp(p, o.p) >= 0; }
  46.  
  47.     // operations between StrType objects and quoted strings
  48.     int operator==(char *s) { return !strcmp(p, s); }
  49.     int operator!=(char *s) { return strcmp(p, s); }
  50.     int operator<(char *s) { return strcmp(p, s) < 0; }
  51.     int operator>(char *s) { return strcmp(p, s) > 0; }
  52.     int operator<=(char *s) { return strcmp(p, s) <= 0; }
  53.     int operator>=(char *s) { return strcmp(p, s) >= 0; }
  54.  
  55.    
  56.     int strsize() { return strlen(p); } // return size of string
  57.     void makestr(char *s) { strcpy(s, p); } // null-terminated string
  58.     operator char *() { return p; } // conversion to char *
  59. };
  60.  
  61. // No explicit initialization.
  62. StrType::StrType() {
  63.     size = 1; // make room for null terminator
  64.     try {
  65.         p = new char[size];
  66.     }
  67.     catch (bad_alloc xa) {
  68.         cout << "Allocation error\n";
  69.         exit(1);
  70.     }
  71.     strcpy(p, "");
  72. }
  73.  
  74. // Initialize using a quoted string.
  75. StrType::StrType(char *str) {
  76.     size = strlen(str) + 1; // make room for null terminator
  77.     try {
  78.         p = new char[size];
  79.     }
  80.     catch (bad_alloc xa) {
  81.         cout << "Allocation error\n";
  82.         exit(1);
  83.     }
  84.     strcpy(p, str);
  85. }
  86.  
  87. // Initialize using a StrType object.
  88. StrType::StrType(const StrType &o) {
  89.     size = o.size;
  90.     try {
  91.         p = new char[size];
  92.     }
  93.     catch (bad_alloc xa) {
  94.         cout << "Allocation error\n";
  95.         exit(1);
  96.     }
  97.     strcpy(p, o.p);
  98. }
  99.  
  100. // Output a string.
  101. ostream &operator<<(ostream &stream, StrType &o)
  102. {
  103.     stream << o.p;
  104.     return stream;
  105. }
  106.  
  107. // Input a string.
  108. istream &operator>>(istream &stream, StrType &o)
  109. {
  110.     char t[255]; // arbitrary size - change if necessary
  111.     int len;
  112.  
  113.     stream.getline(t, 255);
  114.     len = strlen(t) + 1;
  115.  
  116.     if (len > o.size) {
  117.         delete[] o.p;
  118.         try {
  119.             o.p = new char[len];
  120.         }
  121.         catch (bad_alloc xa) {
  122.             cout << "Allocation error\n";
  123.             exit(1);
  124.         }
  125.         o.size = len;
  126.     }
  127.     strcpy(o.p, t);
  128.     return stream;
  129. }
  130.  
  131. // Assign a StrType object to a StrType object.
  132. StrType StrType::operator=(StrType &o)
  133. {
  134.     StrType temp(o.p);
  135.  
  136.     if (o.size > size) {
  137.         delete[] p; // free old memory
  138.         try {
  139.             p = new char[o.size];
  140.         }
  141.         catch (bad_alloc xa) {
  142.             cout << "Allocation error\n";
  143.             exit(1);
  144.         }
  145.         size = o.size;
  146.     }
  147.  
  148.     strcpy(p, o.p);
  149.     strcpy(temp.p, o.p);
  150.  
  151.     return temp;
  152. }
  153.  
  154. // Assign a quoted string to a StrType object.
  155. StrType StrType::operator=(char *s)
  156. {
  157.     int len = strlen(s) + 1;
  158.     if (size < len) {
  159.         delete[] p;
  160.         try {
  161.             p = new char[len];
  162.         }
  163.         catch (bad_alloc xa) {
  164.             cout << "Allocation error\n";
  165.             exit(1);
  166.         }
  167.         size = len;
  168.     }
  169.     strcpy(p, s);
  170.     return *this;
  171. }
  172. StrType StrType::operator=(const char *s)
  173. {
  174.     int len = strlen(s) + 1;
  175.     if (size < len) {
  176.         delete[] p;
  177.         try {
  178.             p = new char[len];
  179.         }
  180.         catch (bad_alloc xa) {
  181.             cout << "Allocation error\n";
  182.             exit(1);
  183.         }
  184.         size = len;
  185.     }
  186.     strcpy(p, s);
  187.     return *this;
  188. }
  189.  
  190. // Concatenate two StrType objects.
  191. StrType StrType::operator+(StrType &o)
  192. {
  193.     int len;
  194.     StrType temp;
  195.  
  196.     delete[] temp.p;
  197.     len = strlen(o.p) + strlen(p) + 1;
  198.     temp.size = len;
  199.     try {
  200.         temp.p = new char[len];
  201.     }
  202.     catch (bad_alloc xa) {
  203.         cout << "Allocation error\n";
  204.         exit(1);
  205.     }
  206.     strcpy(temp.p, p);
  207.  
  208.     strcat(temp.p, o.p);
  209.  
  210.     return temp;
  211. }
  212.  
  213. // Concatenate a StrType object and a quoted string.
  214. StrType StrType::operator+(char *s)
  215. {
  216.     int len;
  217.     StrType temp;
  218.  
  219.     delete[] temp.p;
  220.  
  221.     len = strlen(s) + strlen(p) + 1;
  222.     temp.size = len;
  223.     try {
  224.         temp.p = new char[len];
  225.     }
  226.     catch (bad_alloc xa) {
  227.         cout << "Allocation error\n";
  228.         exit(1);
  229.     }
  230.     strcpy(temp.p, p);
  231.  
  232.     strcat(temp.p, s);
  233.  
  234.     return temp;
  235. }
  236.  
  237. // Concatenate a quoted string and a StrType object.
  238. StrType operator+(char *s, StrType &o)
  239. {
  240.     int len;
  241.     StrType temp;
  242.  
  243.     delete[] temp.p;
  244.  
  245.     len = strlen(s) + strlen(o.p) + 1;
  246.     temp.size = len;
  247.     try {
  248.         temp.p = new char[len];
  249.     }
  250.     catch (bad_alloc xa) {
  251.         cout << "Allocation error\n";
  252.         exit(1);
  253.     }
  254.     strcpy(temp.p, s);
  255.  
  256.     strcat(temp.p, o.p);
  257.  
  258.     return temp;
  259. }
  260.  
  261. void StrType::operator+=(char * s){
  262.     StrType temp;
  263.     temp = p;
  264.     size = size + strlen(s) + 1;
  265.     p = new char[size];
  266.     strcpy(p, temp.p);
  267.     strcat(p, s);
  268. }
  269.  
  270. void StrType::operator+=(StrType &o){
  271.     StrType temp = p;
  272.     size = size + o.size + 1;
  273.     p = new char[size + o.size + 1];
  274.     strcpy(p, temp.p);
  275.     strcat(p, o.p);
  276.  
  277. }
  278.  
  279. StrType StrType::operator-(StrType &substr)
  280. {
  281.     StrType temp(p);
  282.     char *s1;
  283.     int i, j;
  284.  
  285.     s1 = p;
  286.     for (i = 0; *s1; i++) {
  287.         if (*s1 != *substr.p) { // if not first letter of substring
  288.             temp.p[i] = *s1;  // then copy into temp
  289.             s1++;
  290.         }
  291.         else {
  292.             for (j = 0; substr.p[j] == s1[j] && substr.p[j]; j++);
  293.             if (!substr.p[j]) { // is substring, so remove it
  294.                 s1 += j;
  295.                 i--;
  296.             }
  297.             else { // is not substring, continue copying
  298.                 temp.p[i] = *s1;
  299.                 s1++;
  300.             }
  301.         }
  302.     }
  303.     temp.p[i] = '\0';
  304.     return temp;
  305. }
  306.  
  307. // Subtract quoted string from a StrType object.
  308. StrType StrType::operator-(char *substr)
  309. {
  310.     StrType temp(p);
  311.     char *s1;
  312.     int i, j;
  313.  
  314.     s1 = p;
  315.     for (i = 0; *s1; i++) {
  316.         if (*s1 != *substr) { // if not first letter of substring
  317.             temp.p[i] = *s1; // then copy into temp
  318.             s1++;
  319.         }
  320.         else {
  321.             for (j = 0; substr[j] == s1[j] && substr[j]; j++);
  322.             if (!substr[j]) { // is substring, so remove it
  323.                 s1 += j;
  324.                 i--;
  325.             }
  326.             else { // is not substring, continue copying
  327.                 temp.p[i] = *s1;
  328.                 s1++;
  329.             }
  330.         }
  331.     }
  332.     temp.p[i] = '\0';
  333.     return temp;
  334. }
  335.  
  336. bool StrType::insertstr(StrType &p, StrType &o, int position){
  337.     string s, start, end, middle;
  338.     StrType temp;
  339.     s = p;
  340.     start = s.substr(0, position);
  341.     end = s.substr(position, size);
  342.     s = o.p;
  343.     middle = s.substr(0, o.size);
  344.     s = start + middle + end;
  345.     p = s.c_str();
  346.     size = strlen(p) + 1;
  347.     return true;
  348.    
  349.  
  350. };
  351.  
  352. int find_char(StrType &p, char ch){
  353.     for (int i = 0; i < p.strsize(); i++){
  354.         if (p.p[i] == ch) {
  355.             return i;
  356.         }
  357.     }
  358.     return -1;
  359. }
  360. int main()
  361. {
  362.     StrType s1("ab1231;2"), s2("ab");
  363.     int position = find_char(s1, ';');
  364.     cout << "Original string: " << s1 << endl;
  365.     cout << "Number of symbols before ';' :" << position << endl;
  366.     cout << "Number of symbols after ';' :" << s1.strsize() - position - 1 << endl;
  367.     system("pause");
  368.     return 0;
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement