//MyString.h
#include"List.cpp"
class MyString
{
public:
MyString();
~MyString();
MyString(const MyString ©);
MyString(const char *copy);
MyString(List<char>©);
void operator=(const MyString ©);
const char *c_str() const;
protected:
char *entries;
int length;
};
bool operator==(const MyString &first, const MyString &second);
bool operator>=(const MyString &first, const MyString &second);
bool operator<=(const MyString &first, const MyString &second);
bool operator>(const MyString &first, const MyString &second);
bool operator<(const MyString &first, const MyString &second);
bool operator!=(const MyString &first, const MyString &second);
void strcat(MyString &add_to, const MyString &add_on);
MyString read_in(istream &input, int &terminator);
void strcpy(MyString ©, const MyString &original);
void strncpy(MyString ©, const MyString &original, int n);
int strstr(const MyString &text, const MyString &target);
MyString operator+(MyString s1, MyString s2);
ostream &operator<<(ostream & output, MyString s1);
void write(MyString &s);
//MyString.cpp
#define _CRT_SECURE_NO_WARNINGS
#include"MyString.h"
#include<iostream>
using namespace std;
MyString::MyString()
{
entries = new char[1];
entries[0] = '\0';
length = 0;
}
MyString::~MyString()
{
delete[]entries;
}
MyString::MyString(const MyString & copy)
{
length = copy.length;
entries = new char[length + 1];
for (int i = 0; i < length; i++) entries[i] = copy.entries[i];
entries[length] = '\0';
}
MyString::MyString(const char * copy)
{
length = strlen(copy);
entries = new char[length + 1];
strcpy(entries, copy);
}
MyString::MyString(List<char>& copy)
{
length = copy.size();
entries = new char[length + 1];
for (int i = 0; i < length; i++)copy.retrieve(i, entries[i]);
entries[length] = '\0';
}
void MyString::operator=(const MyString & copy)
{
if (entries == copy.entries)return;
delete[] entries;
length = copy.length;
entries = new char[length + 1];
for (int i = 0; i < length; i++)
entries[i] = copy.entries[i];
entries[length] = '\0';
}
const char * MyString::c_str() const
{
return (const char *)entries;
}
bool operator==(const MyString & first, const MyString & second)
{
return (strcmp(first.c_str(),second.c_str())==0);
}
bool operator>=(const MyString & first, const MyString & second)
{
return (strcmp(first.c_str(), second.c_str()) >= 0);
}
bool operator<=(const MyString & first, const MyString & second)
{
return (strcmp(first.c_str(), second.c_str()) <= 0);
}
bool operator>(const MyString & first, const MyString & second)
{
return (strcmp(first.c_str(), second.c_str()) > 0);
}
bool operator<(const MyString & first, const MyString & second)
{
return (strcmp(first.c_str(), second.c_str()) < 0);
}
bool operator!=(const MyString & first, const MyString & second)
{
return (strcmp(first.c_str(), second.c_str()) != 0);
}
void strcat(MyString & add_to, const MyString & add_on)
{
const char *cfirst = add_to.c_str();
const char *csecond = add_on.c_str();
char *copy = new char[strlen(cfirst) + strlen(csecond) + 1];
strcpy(copy, cfirst);
strcpy(copy, csecond);
add_to = copy;
delete []copy;
}
MyString read_in(istream & input, int & terminator)
{
List<char>temp;
terminator = 0;
char c;
while ((c = input.peek()) != EOF && (c = input.get()) != '\n')
temp.insert(terminator++, c);
MyString answer(temp);
return answer;
}
void strcpy(MyString & copy, const MyString & original)
{
if (copy.c_str() == original.c_str())return;
int length = strlen(original.c_str());
char *temp = new char[length + 1];
strcpy(temp, original.c_str());
copy = temp;
delete[] temp;
}
void strncpy(MyString & copy, const MyString & original, int n)
{
char *temp = new char[n + 1];
strncpy(temp, original.c_str(),n);
temp[n] = '\0';
}
int strstr(const MyString & text, const MyString & target)
{
const char *s=strstr(text.c_str(), target.c_str());
if (s == NULL)return -1;
else return(s - target.c_str());
}
MyString operator+(MyString s1, MyString s2)
{
strcat(s1, s2);
return s1;
}
ostream & operator<<(ostream & output, MyString s1)
{
output << s1.c_str() << endl;
return output;
}
void write(MyString & s)
{
cout << s.c_str() << endl;
}
//main.cpp
#include<iostream>
#include"MyString.h"
using namespace std;
void main() {
MyString s1 = "s1 MyString";
cout << s1.c_str() << endl;
cout << strlen(s1.c_str()) << endl;
MyString s2("s2 MyString");
cout << s2.c_str() << endl;
cout << strlen(s2.c_str()) << endl;
if (s1 >= s2) cout << "s1>=s2" << endl;
else cout << "s1<s2" << endl;
s2 = s1;
if (s1 == s2) cout << "s1=s2" << endl;
else cout << "s1!=s2" << endl;
int t;
MyString s3 = read_in(cin,t);
strcat(s3, s1);
write(s3);
}