Skip to content

Instantly share code, notes, and snippets.

/MyString.cpp Secret

Created September 24, 2013 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/8d84b21be6d1f4bc18bf to your computer and use it in GitHub Desktop.
Save anonymous/8d84b21be6d1f4bc18bf to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
#include "MyString.h"
int main()
{
MyString a("hello"), // Initializes using cString
b = " world"; // Assigns value using assignment operator
cout << "a (f): ";
a.put(); // prints a
cout << endl << "a (r): ";
a.reverse(); // prints a in reverse
cout << endl;
MyString c;
c = a + b; // concatenation of two MyStrings
cout << "c: " << c << endl; // printing of c using insertion operator
MyString d;
d = a + " there"; // concatenation of MyString and a cString
cout << "d: " << d << endl;
MyString e;
e = c.substr(3); // Creation of substring using only position
cout << "e: " << e << endl;
MyString f;
f = c.substr(3, 5); // Creation of substring using position and length
cout << "f: " << f << endl;
MyString g(a); // Initialization via copy constructor
cout << "g: " << g << endl;
MyString h = b; // Assigns value using assignment operator
cout << "h: " << h << endl;
cout << "g + h: " << g + h << endl;
MyString i;
cout << "Enter a string: ";
cin >> i;
cout << "i: " << i << endl;
/* Error checking, ensuring that substr will work
MyString i = a.substr(7);
MyString j = a.substr(5, 9);
*/
return 0;
}
#include <iostream>
#include <cassert>
using namespace std;
#include "MyString.h"
MyString::MyString()
{
length = 0;
pData = new char[length + 1];
pData[length] = '\0';
}
MyString::MyString(const char* cString)
{
length = 0;
do
{
length++;
} while (cString[length] != '\0');
pData = new char[length + 1];
for (int i = 0; i < length; i++)
{
pData[i] = cString[i];
}
pData[length] = '\0';
}
MyString::~MyString()
{
delete [] pData;
*pData = NULL;
length = 0;
}
MyString::MyString(const MyString& s)
{
copy(s);
}
MyString MyString::operator=(const MyString& s)
{
copy(s);
return *this;
}
MyString MyString::operator=(const char* cString)
{
MyString temp(cString);
return temp;
}
void MyString::copy(const MyString& s)
{
length = s.length;
pData = new char[length + 1];
for (int i = 0; i < length; i++)
{
pData[i] = s.pData[i];
}
pData[length] = '\0';
}
void MyString::put()
{
for (int i = 0; i < length; i++)
{
cout << pData[i];
}
}
void MyString::reverse()
{
for (int i = length-1; i >= 0; i--)
{
cout << pData[i];
}
}
MyString MyString::operator+(const MyString& s)
{
MyString cat;
cat.length = length + s.length;
cat.pData = new char[cat.length + 1];
for (int i = 0; i < length; i++)
{
cat.pData[i] = pData[i];
}
for (int i = 0; i < s.length; i++)
{
cat.pData[length + i] = s.pData[i];
}
cat.pData[cat.length] = '\0';
return cat;
}
MyString MyString::operator+(const char* cString)
{
MyString temp(cString);
MyString cat;
cat = operator+(temp);
return cat;
}
MyString MyString::substr(const int pos)
{
assert (pos < length);
MyString temp;
temp.length = length - pos;
temp.pData = new char[temp.length + 1];
for (int i = 0; i < temp.length; i++)
{
temp.pData[i] = pData[pos + i];
}
temp.pData[temp.length] = '\0';
return temp;
}
MyString MyString::substr(const int pos, const int len)
{
assert(pos < length);
assert(pos + len <= length);
MyString temp;
temp.length = len;
temp.pData = new char[temp.length + 1];
for (int i = 0; i < temp.length; i++)
{
temp.pData[i] = pData[pos + i];
}
temp.pData[temp.length] = '\0';
return temp;
}
ostream& operator<<(ostream& os, const MyString& s)
{
for (int i = 0; i < s.length; i++)
{
os << s.pData[i];
}
return os;
}
#ifndef __MYSTRING_H__
#define __MYSTRING_H__
class MyString
{
private:
char* pData;
int length;
public:
MyString();
MyString(const char* cString);
~MyString();
MyString(const MyString& s);
MyString operator=(const MyString& s);
MyString operator=(const char* cString);
void put();
void reverse();
MyString operator+(const MyString& s);
MyString operator+(const char* cString);
MyString substr(const int pos);
MyString substr(const int pos, const int len);
friend ostream& operator<<(ostream& os, const MyString& s);
friend istream& operator>>(istream& is, MyString& s);
private:
void copy(const MyString& s);
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment