#include
#include
#include
class MyString {
private:
char *p;
int size;
public:
MyString ();
MyString (int num);
MyString (char *str);
MyString (const MyString &o);
~MyString() {delete [] p;}
friend ostream &operator << (ostream &stream, MyString &o);
friend istream &operator >> (istream &stream, MyString &o);
MyString operator = (MyString &o);
MyString operator = (char *s);
MyString operator + (MyString &o);
MyString operator + (char *s);
friend MyString operator + (char *s, MyString &o);
MyString operator - (MyString &substr);
MyString operator - (char *substr);
//relational operations between MyString
// objects
int operator == (MyString &o) {return !strcmp (p, o.p) ;}
int operator != (MyString &o) {return strcmp (p, o.p) ;}
int operator < (MyString &o) {return strcmp (p, o.p) < 0;}
int operator > (MyString &o) {return strcmp (p, o.p) > 0;}
int operator <= (MyString &o) {return strcmp (p, o.p) <= 0;}
int operator >= (MyString &o) {return strcmp (p, o.p) >= 0;}
//operations between MyString objects an
// d char*
int operator == (char *s) {return !strcmp (p, s) ;}
int operator != (char *s) {return strcmp (p, s) ;}
int operator < (char *s) {return strcmp (p, s) < 0;}
int operator > (char *s) {return strcmp (p, s) > 0;}
int operator <= (char *s) {return strcmp (p, s) <= 0;}
int operator >= (char *s) {return strcmp (p, s) >= 0;}
int strsize () {return strlen (p);}
void makestr (char *s) {strcpy (s, p);}
operator char *() {return p;} //conversion to char*
};
//No explicit initialization
MyString::MyString() {
size = 1; //make room for null terminator
try {
p = new char[size];
}catch (...) {
cout << "Allocation error\n";
exit(1);
}
strcpy (p, "");
}
// Initialize with reserving memory
MyString::MyString (int num) {
size = num + 1;
try {
p = new char[size];
}catch (...) {
cout << "Allocation error\n";
exit (1);
}
}
//Initialize using a quoted string
MyString::MyString (char *str) {
size = strlen (str) + 1; //make room for null terminator
try {
p = new char[size];
}catch (...) {
cout << "Allocation error\n";
exit (1);
}
strcpy (p, str);
}
//Initialize using a MyString object
MyString::MyString (const MyString &o) {
size = o.size;
try {
p = new char [size];
}catch (...) {
cout << "Allocation error\n";
exit (1);
}
strcpy (p, o.p);
}
//Output of strings
ostream &operator << (ostream &stream, MyString &o) {
stream << o.p;
return stream;
}
//Input of strings
istream &operator >> (istream &stream, MyString &o) {
char t[255]; //arbitar size - change if necessary
int len;
stream.getline (t, 255);
len = strlen (t) + 1;
if (len > o.size) {
delete [] o.p;
try {
o.p = new char [len];
}catch (...) {
cout << "Allocation erro\n";
exit (1);
}
o.size = len;
}
strcpy (o.p, t);
return stream;
}
//Assign a MyString object to a Mystring
// object
MyString MyString::operator = (MyString &o) {
MyString temp (o.p);
if (o.size > size) {
delete [] p; //free old memory
try {
p = new char[o.size];
}catch (...) {
cout << "Allocation error\n";
exit (1);
}
size = o.size;
}
strcpy (p, o.p);
strcpy (temp.p, o.p);
return temp;
}
//Assign a quoted string to a MyString o
// bject
MyString MyString::operator = (char *s) {
int len = strlen (s) + 1;
if (size < len) {
delete [] p;
try {
p = new char[len];
}catch (...) {
cout << "Allocation error\n";
exit (1);
}
size = len;
}
strcpy (p, s);
return *this;
}
//Concatenat two Mystring objects
MyString MyString::operator + (MyString &o) {
int len;
MyString temp;
delete [] temp.p;
len = strlen (o.p) + strlen (p) + 1;
temp.size = len;
try {
temp.p = new char[len];
}catch (...) {
cout << "Allocation error\n";
exit (1);
}
strcpy (temp.p, p);
strcat (temp.p, o.p);
return temp;
}
//Concatenate a MyString object and a qu
// oted string
MyString MyString::operator + (char *s) {
int len;
MyString temp;
delete [] temp.p;
len = strlen (s) + strlen (p) + 1;
temp.size = len;
try {
temp.p = new char[len];
}catch (...) {
cout << "Allocation error\n";
exit (1);
}
strcpy (temp.p, p);
strcat (temp.p, s);
return temp;
}
//Concatenates a quoted string and a MyS
// tring object
MyString operator + (char *s, MyString &o) {
int len;
MyString temp;
delete [] temp.p;
len = strlen (s) + strlen (o.p) + 1;
temp.size = len;
try {
temp.p = new char[len];
} catch (...) {
cout << "Allocation error\n";
exit (1);
}
strcpy (temp.p, s);
strcat (temp.p, o.p);
return temp;
}
//Substract a substring from a string us
// ing MyString objects
MyString MyString::operator - (MyString &substr) {
MyString temp (p);
char *s1;
int i, j;
s1 = p;
for (i=0; *s1; i++) {
if (*s1 != *substr.p) { //if not first letter of substring
temp.p[i] = *s1;//then copy into temp
s1++;
}
else {
for (j=0; substr.p[j] == s1[j] && substr.p[j]; j++) ;
if (!substr.p[j]) { //is substring, so remove it
s1 += j;
i--;
}
else { //is not substring, continue copying
temp.p[i] = *s1;
s1++;
}
}
}
temp.p[i] = '\0';
return temp;
}
//Substract quoted string from a MyStrin
// g object
MyString MyString::operator - (char *substr) {
MyString temp (p);
char *s1;
int i, j;
s1 = p;
for (i=0; *s1; i++) {
if (*s1 != *substr) { // if not first letter of substring
temp.p[i] = *s1; // then copy into temp
s1++;
}
else {
for (j=0; substr[j]==s1[j]; j++) ;
if (!substr[j]) { //is substring, so remove it
s1 += j;
i--;
}
else { // is not substring, continue copying
temp.p[i] = *s1;
s1++;
}
}
}
temp.p[i] = '\0';
return temp;
}