create My own String Class in C++ and separate header file and implementation.
MyString.h
#include<iostream> // www.learning2night.com using namespace std; class MyString { private: char* nstring; short nlength; public: //default sets string to NULL MyString(); //Takes in a string and creates an object MyString(const char* ); //Copy constructor MyString(const MyString&); //Destructor deletes the nstring at the end ~MyString(); //displays length of string excluding null int length(); // returns a character at specified index char& at(int ); // if string is empty returns true else it returns false bool empty(); // swaps the contents of two strings void swap(MyString& ); // returns an object which is the substring of the object that calls it MyString substr(int, int); //assign operator overload MyString& operator = (const MyString&); // checks if both strings are equal // equal operation overload bool operator ==(const MyString& ); // concate operation MyString operator + (const MyString &);//still buggy // overload ostream for cout << friend ostream& operator<<(ostream&,const MyString& ); //overload operator >> for cin>> friend istream& operator >> (istream&,MyString&); };
MyString.cpp
# include <iostream> # include <cstring> # include "MyString.h" // www.leaning2night.com using namespace std; MyString::MyString() { nlength = 1; nstring = new char[nlength]; nstring[nlength-1] = '\0'; } MyString::MyString(const char *input) { nlength = strlen(input)+1; nstring = new char[nlength]; for(int i = 0; i < (nlength - 1); i++) { nstring[i] = input[i]; } nstring[(nlength-1)] = '\0'; } MyString::MyString(const MyString& S) { nlength = S.nlength; nstring = new char[nlength]; for(int i = 0; i < (nlength); i++) { nstring[i] = S.nstring[i]; } } MyString::~MyString() { delete[] nstring; } int MyString::length() { return this->nlength-1; } char& MyString::at(int index) { return nstring[index]; } bool MyString::empty() { if(nstring[0] == '\0' && nlength <= 1){return true;} else {return false;} } void MyString::swap(MyString &other) { short len = other.nlength; char *temp = new char[len]; // store other string in temp for(int i = 0; i < len; i++){ temp[i] = other.nstring[i]; } // clear old nstring character of other delete[] other.nstring; // create a new array of character other.nstring = new char[this->nlength]; other.nlength = this->nlength; // assign current string to other for(int i=0;i<this->nlength;i++){ other.nstring[i]=this->nstring[i]; } if(other.nstring[this->nlength-1] != '\0'){other.nstring[this->nlength-1] = '\0';} // clear current character array delete[]nstring; this->nlength = len; this->nstring = new char[this->nlength]; for(int j = 0; j < len; j++){ this->nstring[j] = temp[j]; } if(this->nstring[this->nlength-1] != '\0'){this->nstring[this->nlength-1] = '\0';} delete[]temp; } MyString MyString::substr(int index, int length) { short len = length; if(len > this->length()- index){len=this->length()- index;} int j = 0; MyString str; delete[]str.nstring; str.nlength=len+1; str.nstring= new char[str.nlength]; for(int i = index, j = 0; j < len; i++, j++) { str.nstring[j]=nstring[i]; } str.nstring[len]= '\0'; return str; } bool MyString::operator == (const MyString & S) { if(this->nlength != S.nlength) return false ; short len = S.nlength; int i = 0; while(i != len){ if(this->nstring[i] != S.nstring[i]){return false;} i++; } return true; } MyString& MyString::operator =(const MyString &S) { nlength = S.nlength; delete[] nstring; nstring = new char[nlength]; for(int i = 0 ; i < (nlength -1);i++) { nstring[i] = S.nstring[i]; } nstring[(nlength -1)] = '\0'; return *this; } ostream& operator <<(ostream & OS, const MyString& S) { OS << S.nstring; return OS; } istream& operator >>(istream & IS,MyString& S) { char* input = new char[100]; IS.getline(input,100); int i = 0; while(input[i] != '\0') {i++;} S.nlength = i+1; delete[]S.nstring; S.nstring = new char[(i+1)]; for(int j = 0; j < (i) ; j++) { S.nstring[j] = input[j]; } S.nstring[i] = '\0'; delete[]input; return IS; } MyString MyString::operator + (const MyString& S) { int size = this->nlength + S.nlength +1; if(this->nstring[this->nlength-1]=='\0'){ size--; } if(S.nstring[S.nlength-1]=='\0'){ size--; } MyString str; str.nlength = size; delete[] str.nstring; str.nstring = new char[size]; unsigned current_index =0; for(int i = 0; i < (this->nlength -1) & this->nstring[i]!= '\0';i++) { str.nstring[i] = this->nstring[i]; current_index++; } for(int i = 0; i < (S.nlength -1) & S.nstring[i]!= '\0';i++) { str.nstring[current_index] = S.nstring[i]; current_index++; } str.nstring[current_index] = '\0'; return str; }
File : main.cpp
#include<iostream> #include "MyString.h" using namespace std; int main(){ // assign operator MyString str1 ="learning2night.com"; // constructor MyString str2("hello world"); // ostream operator cout<<str1<<endl<<str2<<endl; // compare 1/true if str1 equal str2 otherwise 0/false cout<<"compare "<<(str1==str2)<<endl; // get character at index cout<<"str1.at(3) = "<<str1.at(3)<<endl; if(str2.empty()) cout<<"str2 is empty !"<<endl; else cout<<"str2 is not empty !"<<endl; // void swap(MyString& ); str1.swap(str2); cout<< "str1 after swap = "<<str1<<endl; cout<< "str2 after swap = "<<str2<<endl; //substring cout<<str2.substr(2,4)<<endl; //concate str2 with substring of str1 MyString str3=str1.substr(0,6) + str2; cout<<str3<<endl; return 0; }