Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- class mystring {
- private:
- char *str;
- int countChars;
- public:
- friend ostream& operator<<(ostream &out, const mystring& string);
- mystring operator* (int val)
- {
- char* buffer = new char[countChars * val + 1];
- for (int i = 0; i < val; ++i)
- {
- for (int j = 0; j < countChars; ++j)
- {
- buffer[i*countChars + j] = str[j];
- }
- }
- buffer[countChars*val + 1] = '\0';
- return mystring(buffer);
- }
- int Count(){
- return countChars;
- }
- bool HaveSubstring(const mystring& substring) const{ //Ïåðåäà÷à ïî ññûëêå, ÷òîáû íå áûëî ëèøíåãî êîïèðîâàíèÿ
- int posSubstring = 0;
- for (int i = 0; i < countChars; ++i)
- {
- if (str[i] == substring.str[posSubstring]){
- posSubstring++;
- if (posSubstring == substring.countChars)
- return true;
- }
- else
- posSubstring = 0;
- }
- return false;
- }
- mystring(const char *c = "")
- {
- str = new char[strlen(c)+1];
- register int i = 0;
- while(c[i])
- {
- str[i] = c[i];
- i++;
- }
- countChars = i;
- str[i] = '\0';
- }
- ~mystring() { cout << "destruct\n"; delete [] str; }
- };
- ostream& operator<<(ostream &out, const mystring& string)
- {
- for (int i = 0; string.str[i]; ++i)
- {
- out << string.str[i];
- }
- return out;
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- {
- mystring a("Hello, friend."), c;
- cout << a << "\n" << endl;
- mystring b("friend");
- cout << "Prefix ((if yes: 1) or (if no: 0)): " << a.HaveSubstring(b) << endl;
- cout << "Count symbol: " << a.Count() << endl;
- cout << a*3 << "\n" << endl;
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement