Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. class mystring {
  7.     private:
  8.         char *str;
  9.         int countChars;
  10.        
  11.     public:
  12.         friend ostream& operator<<(ostream &out, const mystring& string);
  13.         mystring operator* (int val)
  14.         {
  15.             char* buffer = new char[countChars * val + 1];
  16.             for (int i = 0; i < val; ++i)
  17.             {
  18.                 for (int j = 0; j < countChars; ++j)
  19.                 {
  20.                     buffer[i*countChars + j] = str[j];
  21.                 }
  22.             }
  23.             buffer[countChars*val + 1] = '\0';
  24.             return mystring(buffer);
  25.         }
  26.        
  27.          int Count(){
  28.             return countChars;
  29.         }
  30.        
  31.         bool HaveSubstring(const mystring& substring) const{ //Ïåðåäà÷à ïî ññûëêå, ÷òîáû íå áûëî ëèøíåãî êîïèðîâàíèÿ
  32.  
  33.              int posSubstring = 0;
  34.              for (int i = 0; i < countChars; ++i)
  35.              {
  36.                  if (str[i] == substring.str[posSubstring]){
  37.                     posSubstring++;
  38.                     if (posSubstring == substring.countChars)
  39.                     return true;
  40.                  }
  41.              else
  42.                  posSubstring = 0;
  43.              }
  44.  
  45.              return false;
  46.              }
  47.        
  48.         mystring(const char *c = "")
  49.         {
  50.             str = new char[strlen(c)+1];
  51.             register int i = 0;
  52.             while(c[i])
  53.             {
  54.                 str[i] = c[i];
  55.                 i++;
  56.             }
  57.             countChars = i;
  58.             str[i] = '\0';
  59.         }
  60.  
  61.         ~mystring() { cout << "destruct\n"; delete [] str; }
  62. };
  63.  
  64. ostream& operator<<(ostream &out, const mystring& string)
  65. {
  66.     for (int i = 0; string.str[i]; ++i)
  67.     {
  68.         out << string.str[i];
  69.     }
  70.     return out;
  71. }
  72.  
  73. int main()
  74. {
  75.     setlocale(LC_ALL, "Russian");
  76.     {
  77.         mystring a("Hello, friend."), c;
  78.        
  79.        
  80.        
  81.         cout << a << "\n" << endl;  
  82.        
  83.         mystring b("friend");
  84.         cout << "Prefix ((if yes: 1) or (if no: 0)): " << a.HaveSubstring(b) << endl;
  85.              
  86.         cout << "Count symbol: " << a.Count() << endl;
  87.         cout << a*3 << "\n" << endl;
  88.  
  89.     }
  90.     system("pause");
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement