Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // MyString.cpp
- // lab1
- //
- // Created by Alex on 10/2/12.
- // Copyright (c) 2012 Alexey Oshevnev. All rights reserved.
- //
- #include "MyString.h"
- #include <string.h>
- #include <iostream>
- using namespace std;
- MyString::MyString() {
- str = NULL;
- }
- MyString::~MyString() {
- delete[] str;
- }
- MyString::MyString (const MyString &obj) {
- str = new char[strlen(obj.str) + 1];
- strcpy(str, obj.str);
- }
- void MyString :: input() {
- char *tmp = new char[1];
- cout << endl << "input: ";
- fflush(stdin);
- gets(tmp);
- str = new char[strlen(tmp) + 1];
- strcpy(str, tmp);
- }
- void MyString :: output() {
- cout << endl << "output: " << str <<endl;
- }
- MyString MyString::operator+(MyString obj) {
- MyString tmp;
- tmp.str= new char[strlen(this->str)+strlen(obj.str) + 1];
- strcpy(tmp.str,this->str);
- strcat(tmp.str,obj.str);
- return tmp;
- }
- void MyString::operator+=(MyString obj) {
- strcat(this->str, obj.str);
- }
- MyString MyString::operator=(MyString obj) {
- str = new char[strlen(obj.str) + 1];
- strcpy(str, obj.str);
- return *this;
- }
- bool MyString::operator==(MyString obj) {
- return !(strcmp(this->str, obj.str));
- }
- bool MyString::operator!=(MyString obj) {
- return (strcmp(this->str, obj.str));
- }
- bool MyString::operator<(MyString obj) {
- if (strcmp(this->str, obj.str) >= 0)
- return false;
- else
- return true;
- }
- bool MyString::operator>(MyString obj) {
- if (strcmp(this->str, obj.str) >= 0)
- return true;
- else
- return false;
- }
- char MyString::operator[](int counter)
- {
- if (strlen(this->str) < counter || counter < 0)
- return '?';
- else
- return this->str[counter];
- }
- char* MyString::operator()(int begin, int end) {
- if (begin > end || strlen(this->str) < end) {
- char *error = new char[1]; error = "?";
- return error;
- }
- else {
- int count = 0;
- char *tmp = new char[end - begin];
- for (int i = begin; i < end; i++) {
- tmp[count] = this->str[i];
- count++;
- }
- return tmp;
- }
- }
Add Comment
Please, Sign In to add comment