#ifndef MYSTRING_H_INCLUDED
#define MYSTRING_H_INCLUDED
#include <iostream>
#include <string.h>
using namespace std;
class String {
public:
String();
String(const String& s);
String(const char* s);
String& operator=(const String& s);
String& operator=(const char* s);
String& operator+=(const String& s);
friend int operator==(const String& s1, const String& s2);
friend int operator>(const String& s1, const String& s2);
friend int operator<(const String& s1, const String& s2);
char& operator[](int i);
const char& operator[](int i) const;
friend ostream& operator<<(ostream& os, String& s);
friend istream& operator>>(istream& is, String& s);
int length() const
{
{
return len;
}
};
~String()
{
delete[]str;
}
private:
char* str;
int len;
};
#endif
#include "String.h"
#include <string.h>
String::String()
{
len = 0;
str = new char[1];
str[0] = '\0';
}
String::String(const String& s) {
if (s.length() == 0) {
String();
}
len = s.len;
str = new char[len + 1];
strcpy(str, s.str);
}
String::String(const char* s) {
if (s == nullptr) {
String();
}
len = strlen(s);
str = new char[len + 1];
strcpy(str, s);
}
String& String::operator=(const String& s)
{
if (this == &s)
{
return *this;
}
delete[]str;
int len = s.length();
str = new char[len + 1];
strcpy(str, s.str);
return *this;
}
String& String::operator=(const char* s) {
delete[]str;
len = strlen(s);
str = new char[len + 1];
strcpy(str, s);
return *this;
}
String& String::operator+=(const String& s) {
String c(*this);
delete[]this->str;
this->str = new char[this->length() + s.length()];
stpcpy(this->str, c.str);
stpcpy(this->str, s.str);
this->len += s.length();
delete[]c.str;
return *this;
}
String& String::operator+=(const String& s) {
String c(*this);
delete[]this->str;
this->str = new char[this->length() + s.length()];
stpcpy(this->str, c.str);
stpcpy(this->str, s.str);
this->len += s.length();
delete[]c.str;
return *this;
}
ostream& operator<<(ostream& os, String& s) {
os << s.str;
return os;
}
istream& operator>>(istream& is, String& s) {
char temp[80];
cin.getline(temp, 80);
s = temp;
return is;
}