summaryrefslogtreecommitdiffstats
path: root/string.h
diff options
context:
space:
mode:
Diffstat (limited to 'string.h')
-rw-r--r--string.h73
1 files changed, 73 insertions, 0 deletions
diff --git a/string.h b/string.h
new file mode 100644
index 0000000..dc9ebfc
--- /dev/null
+++ b/string.h
@@ -0,0 +1,73 @@
+/* string.h
+ *
+ */
+
+#ifndef _MYSTRING_
+#define _MYSTRING_
+
+#include <cstring>
+#include <iostream>
+
+using namespace std;
+
+class MyString
+{
+ friend bool operator==(const MyString&, const MyString &);
+ friend ostream& operator<<(ostream&, const MyString &);
+
+ private:
+ char *str_;
+ int length_;
+ MyString(int);
+
+ public:
+ MyString();
+ MyString(char);
+ MyString(const char *);
+ MyString(const MyString &);
+ MyString& operator=(const MyString &);
+ ~MyString();
+
+ int length() const;
+ int indexOf(char, int = 0) const;
+ bool isSubstring(const MyString &) const;
+ bool isSubstring(const char *) const;
+ MyString& concat(const MyString &);
+ MyString& concat(const char *);
+ void printStr(void) const;
+
+ MyString operator!() const;
+ char& operator[](int);
+ char operator[](int) const;
+};
+
+bool operator==(const MyString&, const MyString &);
+ostream& operator<<(ostream&, const MyString &);
+
+inline MyString::MyString()
+{
+ length_ = 0;
+ str_ = new char[1];
+ str_[0] = '\0';
+}
+
+inline MyString::MyString(char ch)
+{
+ length_ = 1;
+ str_ = new char[2];
+ str_[0] = ch;
+ str_[1] = '\0';
+}
+
+inline int MyString::length() const
+{
+ return length_;
+}
+
+inline void MyString::printStr(void) const
+{
+ cout << "length: " << length_ << ", value: \"" << str_ <<"\"";
+}
+
+#endif
+