Created
August 27, 2017 05:07
-
-
Save karthikkondagalla/58658c47871f2d8b7b83476c6942ebe7 to your computer and use it in GitHub Desktop.
Manual String class implementation in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "String.h" | |
#include <cstring> | |
#include <cstdlib> | |
const size_t String::npos=-1;// greatest possible value for type size_t | |
/****************************************************** | |
Function: String | |
Arguments: None | |
Description: Default constructor | |
Return: Nothing | |
*******************************************************/ | |
String :: String () : sz(0) , p(NULL) {}; | |
/****************************************************** | |
Function: String | |
Arguments: const String& s | |
Description: Constructor | |
Return: Nothing | |
*******************************************************/ | |
String :: String (const String& s) | |
{ | |
sz=s.sz; | |
if(sz==0)p=NULL; | |
else | |
{ | |
p = new char[sz]; | |
for (unsigned i = 0 ; i < sz ; i++)p[i] = s.p[i]; | |
} | |
} | |
/****************************************************** | |
Function: String | |
Arguments: const char* cs | |
Description: Constructor. | |
Return: Nothing | |
*******************************************************/ | |
String :: String (const char* cs) | |
{ | |
sz = 0; | |
unsigned i; | |
while (cs[sz] != '\0')sz++; | |
p = new char[sz]; | |
for ( i = 0 ; i < sz ; i++)p[i] = cs[i]; | |
} | |
/****************************************************** | |
Function: String | |
Arguments: const size_t& n , const char& c | |
Description: Constructor. | |
Return: Nothing | |
*******************************************************/ | |
String :: String ( const size_t& n , const char& c ) | |
{ | |
sz=n; | |
p = new char[sz]; | |
for (unsigned i = 0 ; i < sz ; i++)p[i] = c; | |
} | |
/****************************************************** | |
Function: String | |
Arguments: const char* cs, const size_t& n | |
Description: Constructor. | |
Return: Nothing | |
*******************************************************/ | |
String :: String ( const char* cs, const size_t& n ) | |
{ | |
sz =n; | |
p = new char[sz]; | |
for (unsigned i = 0 ; i < sz ; i++)p[i] = cs[i]; | |
} | |
/****************************************************** | |
Function: String | |
Arguments: const String& s, const size_t& pos, const size_t& n | |
Description: Constructor. | |
Return: Nothing | |
*******************************************************/ | |
String :: String ( const String& s, const size_t& pos, const size_t& n ) | |
{ | |
sz=n; | |
if(sz==0) p=NULL; | |
else | |
{ | |
p = new char[sz]; | |
unsigned i = pos; | |
unsigned j = n; | |
while(j>0) | |
{ | |
p[n-j] = s.p[i]; | |
i++; | |
j--; | |
} | |
} | |
} | |
/****************************************************** | |
Function: ~String | |
Arguments: None | |
Description: Destructor. | |
Return: Nothing | |
*******************************************************/ | |
String :: ~String () | |
{ | |
clear(); | |
p=NULL; | |
} | |
/****************************************************** | |
Operator: = | |
Arguments: const String* cs | |
Description: Overloading = operator. | |
Return: String& | |
*******************************************************/ | |
String& String :: operator= ( const char* cs ) | |
{ | |
sz = 0; | |
while (cs[sz] != '\0')sz++; | |
p = new char[sz]; | |
for (unsigned i = 0 ; i < sz ; i++)p[i] = cs[i]; | |
return *this; | |
} | |
/****************************************************** | |
Operator: = | |
Arguments: const char& c | |
Description: Overloading = operator. | |
Return: String& | |
*******************************************************/ | |
String& String ::operator= ( const char &c ) | |
{ | |
sz=1; | |
p=new char[sz]; | |
p[0]=c; | |
return *this; | |
} | |
/****************************************************** | |
Operator: = | |
Arguments: const String& s | |
Description: Overloading = operator. | |
Return: String& | |
*******************************************************/ | |
String& String :: operator= ( const String& s ) | |
{ | |
if (p != 0)delete [] p; | |
sz = s.sz; | |
p = new char[sz]; | |
for (unsigned i = 0 ; i < sz ; i++)p[i] = s.p[i]; | |
return *this; | |
} | |
/****************************************************** | |
Operator: + | |
Arguments: const String& s1, const String& s2 | |
Description: Overloading + operator. | |
Return: String | |
*******************************************************/ | |
String operator+ (const String& s1, const String& s2) | |
{ | |
String st; | |
unsigned i; | |
if (s1.sz == 0 && s2.sz == 0) | |
{ | |
st.sz = 0; | |
st.p = NULL; | |
} | |
else | |
{ | |
st.sz = s1.sz + s2.sz; | |
st.p = new char[st.sz]; | |
for (i = 0 ; i < s1.sz ; i++)st.p[i] = s1.p[i]; | |
unsigned j = 0; | |
while (j < s2.sz) | |
{ | |
st.p[i] = s2.p[j]; | |
i++; | |
j++; | |
} | |
} | |
return st; | |
} | |
/****************************************************** | |
Operator: + | |
Arguments: const String& s, const char* cs | |
Description: Overloading + operator. | |
Return: String | |
*******************************************************/ | |
String operator+ (const String& s, const char* cs) | |
{ | |
String st; | |
unsigned i; | |
unsigned len=0; | |
while(cs[len]!='\0') | |
{ | |
len++; | |
} | |
if (s.sz == 0 && len == 0) | |
{ | |
st.sz = 0; | |
st.p = NULL; | |
} | |
else{ | |
st.sz = s.sz + len; | |
st.p = new char[st.sz]; | |
for (i = 0 ; i < s.sz ; i++)st.p[i] = s.p[i]; | |
unsigned j = 0; | |
while (j < len) | |
{ | |
st.p[i] = cs[j]; | |
i++; | |
j++; | |
} | |
} | |
return st; | |
} | |
/****************************************************** | |
Operator: + | |
Arguments: const char* cs, const String& s | |
Description: Overloading + operator. | |
Return: String | |
*******************************************************/ | |
String operator+ (const char* cs, const String& s) | |
{ | |
String st; | |
unsigned i; | |
unsigned len=0; | |
while(cs[len]!='\0') | |
{ | |
len++; | |
} | |
if (s.sz == 0 && len == 0) | |
{ | |
st.sz = 0; | |
st.p = NULL; | |
} | |
else | |
{ | |
st.sz = s.sz + len; | |
st.p = new char[st.sz]; | |
for (i = 0 ; i < len ; i++)st.p[i] = cs[i]; | |
unsigned j = 0; | |
while (j < s.sz) | |
{ | |
st.p[i] = s.p[j]; | |
i++; | |
j++; | |
} | |
} | |
return st; | |
} | |
/****************************************************** | |
Operator: + | |
Arguments: const String& s, const char& c | |
Description: Overloading + operator. | |
Return: String | |
*******************************************************/ | |
String operator+ (const String& s, const char& c) | |
{ | |
String st; | |
unsigned i; | |
if (s.sz == 0) | |
{ | |
st.sz = 0; | |
st.p = NULL; | |
} | |
else | |
{ | |
st.sz = s.sz + 1; | |
st.p = new char[st.sz]; | |
for (i = 0 ; i < s.sz ; i++)st.p[i] = s.p[i]; | |
st.p[i] = c; | |
} | |
return st; | |
} | |
/****************************************************** | |
Operator: + | |
Arguments: const char& c, const String& s | |
Description: Overloading + operator. | |
Return: String | |
*******************************************************/ | |
String operator+ (const char& c, const String& s) | |
{ | |
String st; | |
unsigned i; | |
if (s.sz == 0) | |
{ | |
st.sz = 0; | |
st.p = NULL; | |
} | |
else | |
{ | |
st.sz = s.sz + 1; | |
st.p = new char[st.sz]; | |
st.p[0] = c; | |
for (i = 1 ; i < s.sz ; i++)st.p[i] = s.p[i]; | |
} | |
return st; | |
} | |
/****************************************************** | |
Operator: += | |
Arguments: const String& s | |
Description: Overloading =+ operator. | |
Return: String& | |
*******************************************************/ | |
String& String :: operator+=(const String& s) | |
{ | |
String st; | |
unsigned i; | |
if (sz == 0 && s.sz == 0) | |
{ | |
st.sz = 0; | |
st.p = NULL; | |
} | |
else | |
{ | |
st.sz = sz + s.sz; | |
st.p = new char[st.sz]; | |
for (i = 0 ; i < sz ; i++)st.p[i] = p[i]; | |
unsigned j = 0; | |
while (j < s.sz) | |
{ | |
st.p[i] = s.p[j]; | |
i++; | |
j++; | |
} | |
} | |
clear(); | |
p=new char[st.sz]; | |
sz=st.sz; | |
for (i = 0 ; i < sz ; i++)p[i] = st.p[i]; | |
return *this; | |
} | |
/****************************************************** | |
Operator: += | |
Arguments: const char* cs | |
Description: Overloading += operator. | |
Return: String& | |
*******************************************************/ | |
String& String :: operator+= (const char* cs) | |
{ | |
String st; | |
unsigned len=0; | |
unsigned i; | |
while(cs[len]!='\0') | |
{ | |
len++; | |
} | |
if (sz == 0 && len == 0) | |
{ | |
st.sz = 0; | |
st.p = NULL; | |
} | |
else | |
{ | |
st.sz = sz + len; | |
st.p = new char[st.sz]; | |
for (i = 0 ; i < sz ; i++)st.p[i] = p[i]; | |
unsigned j = 0; | |
while (j < len) | |
{ | |
st.p[i] = cs[j]; | |
i++; | |
j++; | |
} | |
} | |
clear(); | |
sz=st.sz; | |
p=new char[st.sz]; | |
for (i = 0 ; i < sz ; i++)p[i] = st.p[i]; | |
return *this; | |
} | |
/****************************************************** | |
Operator: += | |
Arguments: const char& c | |
Description: Overloading += operator. | |
Return: String& | |
*******************************************************/ | |
String& String :: operator+= (const char& c) | |
{ | |
String st; | |
unsigned len=1; | |
unsigned i; | |
if (sz == 0) | |
{ | |
st.sz = 0; | |
st.p = NULL; | |
} | |
else | |
{ | |
st.sz = sz + len; | |
st.p = new char[st.sz]; | |
for (i = 0 ; i < sz ; i++)st.p[i] = p[i]; | |
st.p[i]=c; | |
} | |
clear(); | |
sz=st.sz; | |
p=new char[st.sz]; | |
for (i = 0 ; i < sz ; i++)p[i] = st.p[i]; | |
return *this; | |
} | |
/****************************************************** | |
Operator: == | |
Arguments: const String& s1, const String& s2 | |
Description: Overloading == operator. | |
Return: True or false | |
*******************************************************/ | |
bool operator== (const String& s1, const String& s2) | |
{ | |
bool flag; | |
if (s1.size() == s2.size()){ | |
for (unsigned i = 0 ; i < s1.size() ; i++) | |
{ | |
if (s1.p[i] != s2.p[i]) | |
flag = 0; | |
} | |
} | |
else | |
flag = 0; | |
return flag; | |
} | |
/****************************************************** | |
Operator: != | |
Arguments: const String& s1, const String& s2 | |
Description: Overloading != operator. | |
Return: True or false | |
*******************************************************/ | |
bool operator!= (const String& s1, const String& s2) | |
{ | |
bool flag; | |
if (s1.size() != s2.size()) | |
flag = 1; | |
else | |
{ | |
for (unsigned i = 0 ; i < s1.size() ; i++) | |
if (s1.p[i] != s2.p[i]) | |
flag = 1; | |
} | |
return flag; | |
} | |
/****************************************************** | |
Operator: < | |
Arguments: const String& s1, const String& s2 | |
Description: Overloading < operator. | |
Return: True or false | |
*******************************************************/ | |
bool operator<(const String& s1, const String& s2) | |
{ | |
bool flag=false; | |
for (unsigned i = 0 ; i < s2.size() ; i++) | |
{ | |
if (s1.p[i] < s2.p[i]) | |
{ | |
return true; | |
} | |
else if(s1.p[i] == s2.p[i]){} | |
else{ | |
return false; | |
} | |
} | |
return flag; | |
} | |
/****************************************************** | |
Operator: <= | |
Arguments: const String& s1, const String& s2 | |
Description: Overloading <= operator. | |
Return: True or false | |
*******************************************************/ | |
bool operator<= (const String& s1, const String& s2) | |
{ | |
bool flag=false; | |
for (unsigned i = 0 ; i < s2.size() ; i++) | |
{ | |
if (s1.p[i] < s2.p[i]) | |
{ | |
return true; | |
break; | |
} | |
else if(s1.p[i] == s2.p[i]) | |
{ | |
flag=true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
return flag; | |
} | |
/****************************************************** | |
Operator: > | |
Arguments: const String& s1, const String& s2 | |
Description: Overloading > operator. | |
Return: True or false | |
*******************************************************/ | |
bool operator> (const String& s1, const String& s2) | |
{ | |
bool flag=false; | |
for (unsigned i = 0 ; i < s2.size() ; i++) | |
{ | |
if (s1.p[i] > s2.p[i]) | |
{ | |
return true; | |
} | |
else if(s1.p[i] == s2.p[i]){} | |
else{ | |
return false; | |
} | |
} | |
return flag; | |
} | |
/****************************************************** | |
Operator: >= | |
Arguments: const String& s1, const String& s2 | |
Description: Overloading >= operator. | |
Return: True or false | |
*******************************************************/ | |
bool operator>= (const String& s1, const String& s2) | |
{ | |
bool flag=false; | |
for (unsigned i = 0 ; i < s2.size() ; i++) | |
{ | |
if (s1.p[i] > s2.p[i]) | |
{ | |
return true; | |
} | |
else if(s1.p[i] == s2.p[i]){ | |
flag=true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
return flag; | |
} | |
/****************************************************** | |
Operator: << | |
Arguments: ostream& os, const String& s | |
Description: Overloading << operator. | |
Return: ostream& | |
*******************************************************/ | |
ostream& operator<< ( ostream& os, const String& s ) | |
{ | |
for (unsigned i = 0 ; i < s.sz ; i++)os << s.p[i]; | |
return os; | |
} | |
/****************************************************** | |
Operator: >> | |
Arguments: ostream& os, const String& s | |
Description: Overloading >> operator. | |
Return: istream& | |
*******************************************************/ | |
istream& operator>> ( istream& is, String& s ) | |
{ | |
getline(is,s); | |
return is; | |
} | |
/****************************************************** | |
Operator: [] | |
Arguments: const size_t& pos | |
Description: Overloading [] operator. | |
Return: char& | |
*******************************************************/ | |
char& String :: operator[] (const size_t& pos) | |
{ | |
if( pos > sz ) | |
cerr << "out_of_range exception: pos="<<pos; | |
return p[pos]; | |
} | |
/****************************************************** | |
Operator: [] | |
Arguments: const size_t& pos | |
Description: Overloading [] operator. | |
Return: const char& | |
*******************************************************/ | |
const char& String :: operator[] (const size_t& pos) const | |
{ | |
if( pos > sz ) | |
{ | |
cerr << "out_of_range exception: pos="<<pos; | |
} | |
return p[pos]; | |
} | |
/****************************************************** | |
Function: length | |
Arguments: None | |
Description: Returns the length. | |
Return: size_t | |
*******************************************************/ | |
size_t String :: length () const | |
{ | |
return sz; | |
} | |
/****************************************************** | |
Function: size | |
Arguments: None | |
Description: Returns the size. | |
Return: size_t | |
*******************************************************/ | |
size_t String :: size () const | |
{ | |
return sz; | |
} | |
/****************************************************** | |
Function: empty | |
Arguments: None | |
Description: Checks if the string is empty or not. | |
Return: True or false | |
*******************************************************/ | |
bool String :: empty () const | |
{ | |
if(p == NULL && sz == 0) return true; | |
else return false; | |
} | |
/****************************************************** | |
Function: clear | |
Arguments: None | |
Description: Clears the string. | |
Return: Void | |
*******************************************************/ | |
void String :: clear () | |
{ | |
if (sz>0) | |
delete [] p; | |
sz=0; | |
p=NULL; | |
} | |
/****************************************************** | |
Function: getline | |
Arguments: istream& is, String& s, const char& del | |
Description: Implimentation for getline | |
Return: istream& | |
*******************************************************/ | |
istream& getline ( istream& is, String& s, const char& del) | |
{ | |
s.clear(); | |
char ch=getchar(); | |
while(ch != del){ | |
s.push_back(ch); | |
ch=getchar(); | |
} | |
return is; | |
} | |
/****************************************************** | |
Function: at | |
Arguments: const size_t& pos | |
Description: Implimentation for at | |
Return: char& | |
*******************************************************/ | |
char& String :: at ( const size_t& pos) | |
{ | |
if(pos>sz) | |
cerr << "out_of_range exception: pos="<<pos<<endl; | |
return p[pos]; | |
} | |
/****************************************************** | |
Function: at | |
Arguments: const size_t& pos | |
Description: Implimentation for at | |
Return: const char& | |
*******************************************************/ | |
const char& String :: at ( const size_t& pos) const | |
{ | |
if(pos>sz) | |
cerr << "out_of_range exception: pos="<<pos<<endl; | |
return p[pos]; | |
} | |
/****************************************************** | |
Function: substr | |
Arguments: const size_t& pos , const size_t& n | |
Description: Implementation of substr. | |
Return: String | |
*******************************************************/ | |
String String::substr ( const size_t& pos , const size_t& n) | |
{ | |
String st; | |
unsigned i,j; | |
if( pos>sz) | |
{ | |
cerr<<"out_of_range exception: pos = "<<pos<<endl; | |
return st; | |
} | |
if(pos==sz)return st; | |
else | |
{ | |
for( i=pos,j=0 ; (j<n && p[i]!='\0'); i++,j++) | |
{ | |
st.expandMem(1); | |
st.p[j]=p[i]; | |
} | |
} | |
st.p[j]='\0'; | |
return st; | |
} | |
/****************************************************** | |
Function: c_str | |
Arguments: None | |
Description: Implementation of c_str. | |
Return: const char* | |
*******************************************************/ | |
const char* String :: c_str () const | |
{ | |
char *ch=new char[sz+1]; | |
unsigned i; | |
for(i=0;i<sz;i++) | |
ch[i]=p[i]; | |
ch[i]='\0'; | |
return ch; | |
} | |
/****************************************************** | |
Function: data | |
Arguments: None | |
Description: Implementation of data. | |
Return: const char* | |
*******************************************************/ | |
const char* String :: data () const | |
{ | |
char *ch=new char[sz+1]; | |
unsigned i; | |
for(i=0;i<sz;i++) | |
ch[i]=p[i]; | |
ch[i]='\0'; | |
return ch; | |
} | |
/****************************************************** | |
Function: push_back | |
Arguments: const char& | |
Description: Implementation of push_back. | |
Return: Nothing | |
*******************************************************/ | |
void String :: push_back ( const char& c ) | |
{ | |
unsigned pre = sz; | |
expandMem(1); | |
p[pre] = c; | |
} | |
/****************************************************** | |
Function: expandMem | |
Arguments: const size_t& n | |
Description: This function is used to increase the memory. | |
Return: Nothing | |
*******************************************************/ | |
void String :: expandMem ( const size_t& n ) | |
{ | |
unsigned exp = n+sz; | |
char *temp = new char[exp+1]; | |
memset(temp, 0, exp+1); | |
temp[exp] = '\0'; | |
for(unsigned i=0;i<sz;i++) | |
temp[i] = p[i]; | |
clear(); | |
sz = exp; | |
p = new char[sz+1]; | |
memset(p, 0, sz+1); | |
p[sz] = '\0'; | |
for(unsigned i = 0;temp[i]!='\0';i++) | |
p[i] = temp[i]; | |
delete[] temp; | |
temp = NULL; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "String.h" | |
const size_t String::npos=-1; | |
/****************************************************** | |
Function: String | |
Arguments: None | |
Description: Default constructor | |
Return: Nothing | |
*******************************************************/ | |
String::String() | |
{ | |
p=new char[1]; | |
sz=1; | |
p[0]='\0'; | |
} | |
/****************************************************** | |
Function: String | |
Arguments: const String& s | |
Description: Constructor | |
Return: Nothing | |
*******************************************************/ | |
String::String(const String& s) | |
{ | |
unsigned i,j; | |
for(i=0;s.p[i]!='\0';i++); | |
p=new char[i+1]; | |
sz=i+1; | |
for(j=0;j<sz;j++)p[j]=s.p[j]; | |
} | |
/****************************************************** | |
Function: String | |
Arguments: const char* cs | |
Description: Constructor | |
Return: Nothing | |
*******************************************************/ | |
String::String(const char* cs) | |
{ | |
unsigned i,j; | |
for(i=0;cs[i]!='\0';i++); | |
sz=i+1; | |
p=new char[sz]; | |
for(j=0;j<(sz-1);j++)p[j]=cs[j]; | |
p[i]='\0'; | |
} | |
/****************************************************** | |
Function: String | |
Arguments: const String& s,const size_t& pos,const size_t& n | |
Description: Constructor | |
Return: Nothing | |
*******************************************************/ | |
String::String(const String& s,const size_t& pos,const size_t& n) | |
{ | |
unsigned i,j; | |
p=new char[n+1]; | |
sz=n+1; | |
for(i=pos,j=0;i<(pos+n);i++,j++)p[j]=s.p[i]; | |
p[n]='\0'; | |
} | |
/****************************************************** | |
Function: String | |
Arguments: const char* cs,const size_t& n | |
Description: Constructor | |
Return: Nothing | |
*******************************************************/ | |
String::String(const char* cs,const size_t& n) | |
{ | |
unsigned i; | |
for(i=0;cs[i]!='\0';i++); | |
sz=n+1; | |
p=new char[sz]; | |
for(i=0;i<(sz-1)&&i<n;i++)p[i]=cs[i]; | |
p[n]='\0'; | |
} | |
/****************************************************** | |
Function: String | |
Arguments: const size_t& n,const char& c | |
Description: Constructor | |
Return: Nothing | |
*******************************************************/ | |
String::String(const size_t& n,const char& c) | |
{ | |
unsigned i; | |
sz=n+1; | |
p=new char[sz]; | |
for(i=0;i<n;i++)p[i] = c; | |
p[n]='\0'; | |
} | |
/****************************************************** | |
Function: ~String | |
Arguments: None | |
Description: Destructor. | |
Return: Nothing | |
*******************************************************/ | |
String::~String() | |
{ | |
delete [] p; | |
} | |
/****************************************************** | |
Operator: = | |
Arguments: const String& s | |
Description: Overloading = operator. | |
Return: String& | |
*******************************************************/ | |
String& String::operator=(const String& s) | |
{ | |
unsigned i; | |
sz=s.sz; | |
delete [] p; | |
p=new char[sz]; | |
for(i=0;i<(sz-1);i++)p[i]=s.p[i]; | |
p[sz-1]='\0'; | |
return *this; | |
} | |
/****************************************************** | |
Operator: = | |
Arguments: const char* cs | |
Description: Overloading = operator. | |
Return: String& | |
*******************************************************/ | |
String& String::operator=(const char* cs) | |
{ | |
unsigned i,j; | |
for(i=0;cs[i]!='\0';i++); | |
sz=i+1; | |
delete [] p; | |
p=new char[sz]; | |
for(j=0;j<(sz-1);j++)p[j]=cs[j]; | |
p[i]='\0'; | |
return *this; | |
} | |
/****************************************************** | |
Operator: = | |
Arguments: const char& c | |
Description: Overloading = operator. | |
Return: String& | |
*******************************************************/ | |
String& String::operator=(const char& c) | |
{ | |
unsigned i; | |
delete [] p; | |
p=new char[1+1]; | |
for(i=0;i<1;i++)p[i]=c; | |
p[i]='\0'; | |
return *this; | |
} | |
/****************************************************** | |
Operator: += | |
Arguments: const String& s | |
Description: Overloading =+ operator. | |
Return: String& | |
*******************************************************/ | |
String& String::operator+=(const String& s) | |
{ | |
int size=s.sz+sz-1; | |
unsigned i,j,k; | |
char* str=new char[size]; | |
for(i=0;i<sz-1;i++)str[i]=p[i]; | |
for(j=0;j<s.size();j++)str[j+sz-1]=s.p[j]; | |
delete [] p; | |
sz=size; | |
p=new char[sz]; | |
for(k=0;k<sz;k++)p[k]=str[k]; | |
return *this; | |
} | |
/****************************************************** | |
Operator: += | |
Arguments: const char* cs | |
Description: Overloading += operator. | |
Return: String& | |
*******************************************************/ | |
String& String::operator+=(const char* cs) | |
{ | |
unsigned i,j,k,l; | |
for(i=0;cs[i]!='\0';i++); | |
char *str=new char[sz+i]; | |
for(l=0;l<sz-1;l++)str[l]=p[l]; | |
for(j=0;j<=i;j++) | |
str[sz+j-1]=cs[j]; | |
delete [] p; | |
p=new char[sz+i]; | |
for(k=0;k<(sz+i);k++)p[k]=str[k]; | |
sz+=i; | |
return *this; | |
} | |
/****************************************************** | |
Operator: += | |
Arguments: const char& c | |
Description: Overloading += operator. | |
Return: String& | |
*******************************************************/ | |
String& String::operator+=(const char& c) | |
{ | |
unsigned i,k,size=sz+1; | |
char* str=new char[size]; | |
for(i=0;i<(sz-1);i++) | |
str[i]=p[i]; | |
delete [] p; | |
p=new char[size]; | |
sz=size; | |
for(k=0;k<(size-2);k++) | |
p[k]=str[k]; | |
p[size-2]=c; | |
p[size-1]='\0'; | |
return *this; | |
} | |
/****************************************************** | |
Operator: << | |
Arguments: ostream& os, const String& s | |
Description: Overloading << operator. | |
Return: ostream& | |
*******************************************************/ | |
ostream& operator<<(ostream& os,const String& s) | |
{ | |
return os<<s.p; | |
} | |
/****************************************************** | |
Function: empty | |
Arguments: None | |
Description: This method checks whether the string is empty or not. | |
Return: True or False | |
*******************************************************/ | |
bool String::empty() const | |
{ | |
return sz==1; | |
} | |
/****************************************************** | |
Function: length | |
Arguments: None | |
Description: This method returns the length of the string. | |
Return: size_t | |
*******************************************************/ | |
size_t String::length() const | |
{ | |
return sz-1; | |
} | |
/****************************************************** | |
Function: size | |
Arguments: None | |
Description: This method returns the size of the string. | |
Return: size_t | |
*******************************************************/ | |
size_t String::size() const | |
{ | |
return sz-1; | |
} | |
/****************************************************** | |
Function: erase | |
Arguments: const size_t& pos,const size_t& n | |
Description: This method is used to erase a string. | |
Return: String& | |
*******************************************************/ | |
String& String::erase(const size_t& pos,const size_t& n) | |
{ | |
unsigned i,j; | |
String str(p); | |
sz=str.sz-n; | |
delete [] p; | |
p=new char[sz]; | |
for(i=0;i<pos;i++) | |
p[i]=str.p[i]; | |
for(i=pos,j=pos+n;j<str.sz;i++,j++)p[i]=str.p[j]; | |
return *this; | |
} | |
/****************************************************** | |
Function: clear | |
Arguments: None | |
Description: This method is used to delete the memory assigned. | |
Return: Nothing | |
*******************************************************/ | |
void String::clear() | |
{ | |
delete [] p; | |
sz=1; | |
p=new char[sz]; | |
p[0]='\0'; | |
} | |
/****************************************************** | |
Function: copy | |
Arguments: char* cs,const size_t& n,const size_t& pos | |
Description: This method is used to copy a string. | |
Return: size_t | |
*******************************************************/ | |
size_t String::copy(char* cs,const size_t& n,const size_t& pos) const | |
{ | |
size_t i,j; | |
for(j=pos,i=0;i<n;i++,j++) | |
cs[i]=p[j]; | |
return i; | |
} | |
/****************************************************** | |
Function: swap | |
Arguments: String& s | |
Description: This method is used to swap string. | |
Return: Nothing | |
*******************************************************/ | |
void String::swap(String& s) | |
{ | |
size_t size=sz; | |
char str[size]; | |
size_t i=this->copy(str,sz-1,0); | |
str[i]=0; | |
*this=s; | |
s=str; | |
} | |
/****************************************************** | |
Function: assign | |
Arguments: const String& c | |
Description: This method is used to assign string to the current pointer. | |
Return: String& | |
*******************************************************/ | |
String& String::assign(const String& s) | |
{ | |
return *this=s; | |
} | |
/****************************************************** | |
Function: assign | |
Arguments: const char* cs | |
Description: This method is used to assign string to the current pointer. | |
Return: String& | |
*******************************************************/ | |
String& String::assign(const char* cs) | |
{ | |
String str(cs); | |
*this=str; | |
return *this; | |
} | |
/****************************************************** | |
Function: assign | |
Arguments: const char* cs,const size_t& n | |
Description: This method is used to assign string to the current pointer. | |
Return: String& | |
*******************************************************/ | |
String& String::assign(const char* cs,const size_t& n) | |
{ | |
unsigned i; | |
sz=n+1; | |
p=new char[n+1]; | |
for(i=0;i<(sz-1);i++) | |
p[i]=cs[i]; | |
p[sz-1]='\0'; | |
return *this; | |
} | |
/****************************************************** | |
Function: assign | |
Arguments: const size_t& n,const char c | |
Description: This method is used to assign string to the current pointer. | |
Return: String& | |
*******************************************************/ | |
String& String::assign(const size_t& n,const char c) | |
{ | |
String str(n,c); | |
*this=str; | |
return *this; | |
} | |
/****************************************************** | |
Function: assign | |
Arguments: const String& s,const size_t& pos,const size_t& n | |
Description: This method is used to assign string to the current pointer. | |
Return: String& | |
*******************************************************/ | |
String& String::assign(const String& s,const size_t& pos,const size_t& n) | |
{ | |
String str(s,pos,n); | |
unsigned i; | |
delete [] p; | |
sz=str.sz; | |
p=new char[sz]; | |
for(i=0;i<sz;i++) | |
p[i]=str.p[i]; | |
return *this; | |
} | |
/****************************************************** | |
Function: append | |
Arguments: const String& s | |
Description: This method is used to append the given string to an existing string. | |
Return: String& | |
*******************************************************/ | |
String& String::append(const String& s) | |
{ | |
String str; | |
str+=s; | |
*this=str; | |
return *this; | |
} | |
/****************************************************** | |
Function: append | |
Arguments: const char* cs | |
Description: This method is used to append the given string to an existing string. | |
Return: String& | |
*******************************************************/ | |
String& String::append(const char* cs) | |
{ | |
*this+=cs; | |
return *this; | |
} | |
/****************************************************** | |
Function: append | |
Arguments: const String& s,const size_t& pos,const size_t& n | |
Description: This method is used to append the given string to an existing string. | |
Return: String& | |
*******************************************************/ | |
String& String::append(const String& s,const size_t& pos,const size_t& n) | |
{ | |
String str(s,pos,n); | |
*this+=str; | |
return *this; | |
} | |
/****************************************************** | |
Function: append | |
Arguments: const char* cs,const size_t& n | |
Description: This method is used to append the given string to an existing string. | |
Return: String& | |
*******************************************************/ | |
String& String::append(const char* cs,const size_t& n) | |
{ | |
String str(cs,n); | |
*this+=str; | |
return *this; | |
} | |
/****************************************************** | |
Function: append | |
Arguments: const size_t& n,const char& c | |
Description: This method is used to append the given string to an existing string. | |
Return: String& | |
*******************************************************/ | |
String& String::append(const size_t& n,const char& c) | |
{ | |
unsigned i; | |
String str(p); | |
String str1(n,c); | |
str+=str1; | |
delete [] p; | |
sz=str.sz; | |
p=new char[sz]; | |
for(i=0;i<sz;i++) | |
p[i]=str.p[i]; | |
return *this; | |
} | |
/****************************************************** | |
Function: compare | |
Arguments: const char* cs | |
Description: This method is used to compare two strings. | |
Return: int | |
*******************************************************/ | |
int String::compare(const char* cs) const | |
{ | |
String str(cs); | |
return this->compare(str); | |
} | |
/****************************************************** | |
Function: compare | |
Arguments: const String& s | |
Description: This method is used to compare two strings. | |
Return: int | |
*******************************************************/ | |
int String::compare(const String& s) const | |
{ | |
unsigned i,j; | |
int compare; | |
bool less=1; | |
bool greater=1; | |
bool equal=1; | |
if(sz!=s.sz) | |
equal=0; | |
else | |
for(i=0;i<s.size();i++) | |
{ | |
if(p[i]!=s.p[i]) | |
equal=0; | |
} | |
for(i=0,j=0;j<s.size();i++,j++) | |
{ | |
if(p[i]>=s.p[j]) | |
less=0; | |
} | |
if(less||equal) | |
greater=0; | |
if(less) | |
compare=-1; | |
if(greater) | |
compare=1; | |
if(equal) | |
compare=0; | |
return compare; | |
} | |
/****************************************************** | |
Function: compare | |
Arguments: const size_t& pos,const size_t& n,const String& s | |
Description: This method is used to compare two strings. | |
Return: int | |
*******************************************************/ | |
int String::compare(const size_t& pos,const size_t& n,const String& s) const | |
{ | |
String str(p,pos,n); | |
return str.compare(s); | |
} | |
/****************************************************** | |
Function: compare | |
Arguments: const size_t& pos,const size_t& n,const char* cs | |
Description: This method is used to compare two strings. | |
Return: int | |
*******************************************************/ | |
int String::compare(const size_t& pos,const size_t& n,const char* cs) const | |
{ | |
String str(p,pos,n); | |
String str1(cs); | |
return str.compare(str1); | |
} | |
/****************************************************** | |
Function: compare | |
Arguments: const size_t& pos1,const size_t& n1,const String& s,const size_t& pos2,const size_t& n2 | |
Description: This method is used to compare two strings. | |
Return: int | |
*******************************************************/ | |
int String::compare(const size_t& pos1,const size_t& n1,const String& s,const size_t& pos2,const size_t& n2) const | |
{ | |
String str(p,pos1,n1); | |
String str1(s,pos2,n2); | |
return str.compare(str1); | |
} | |
/****************************************************** | |
Function: compare | |
Arguments: const size_t& pos,const size_t& n1,const char* cs,const size_t& n2 | |
Description: This method is used to compare two strings. | |
Return: int | |
*******************************************************/ | |
int String::compare(const size_t& pos,const size_t& n1,const char* cs,const size_t& n2) const | |
{ | |
String str(p,pos,n1); | |
String str1(cs,0,n2); | |
return str.compare(str1); | |
} | |
/****************************************************** | |
Function: insert | |
Arguments: const size_t& pos,const char* cs | |
Description: This method is used to insert a string. | |
Return: String& | |
*******************************************************/ | |
String& String::insert(const size_t& pos,const char* cs) | |
{ | |
String str(cs); | |
String str1(p); | |
*this=str1.insert(pos,str); | |
return *this; | |
} | |
/****************************************************** | |
Function: insert | |
Arguments: const size_t& pos,const String& s | |
Description: This method is used to insert a string. | |
Return: String& | |
*******************************************************/ | |
String& String::insert(const size_t& pos,const String& s) | |
{ | |
String str(p); | |
unsigned i,j; | |
sz=s.sz+str.sz-1; | |
delete [] p; | |
p=new char[sz]; | |
for(i=0;i<pos;i++) | |
p[i]=str.p[i]; | |
for(i=pos,j=0;j<(s.sz-1);i++,j++) | |
p[i]=s.p[j]; | |
for(i=s.sz+pos-1,j=pos;i<sz;i++,j++) | |
p[i]=str.p[j]; | |
return *this; | |
} | |
/****************************************************** | |
Function: insert | |
Arguments: const size_t& pos1,const String& s,const size_t& pos2,const size_t& n | |
Description: This method is used to insert a string. | |
Return: String& | |
*******************************************************/ | |
String& String::insert(const size_t& pos1,const String& s,const size_t& pos2,const size_t& n) | |
{ | |
String str(s,pos2,n); | |
String str1(p); | |
*this=str1.insert(pos1,str); | |
return *this; | |
} | |
/****************************************************** | |
Function: insert | |
Arguments: const size_t& pos,const char* cs,const size_t& n | |
Description: This method is used to insert a string. | |
Return: String& | |
*******************************************************/ | |
String& String::insert(const size_t& pos,const char* cs,const size_t& n) | |
{ | |
String str(cs,n); | |
String str1(p); | |
*this=str1.insert(pos,str); | |
return *this; | |
} | |
/****************************************************** | |
Function: insert | |
Arguments: const size_t& pos,const size_t& n,const char& c | |
Description: This method is used to insert a string. | |
Return: String& | |
*******************************************************/ | |
String& String::insert(const size_t& pos,const size_t& n,const char& c) | |
{ | |
String str(n,c); | |
String str1(p); | |
*this=str1.insert(pos,str); | |
return *this; | |
} | |
/****************************************************** | |
Function: replace | |
Arguments: const size_t& pos,const size_t& n,const String& s | |
Description: This method is used to replace a string. | |
Return: String& | |
*******************************************************/ | |
String& String::replace(const size_t& pos,const size_t& n,const String& s) | |
{ | |
String str(p); | |
unsigned i; | |
str.erase(pos,n); | |
str.insert(pos,s); | |
sz=str.sz; | |
delete [] p; | |
p=new char[sz]; | |
for(i=0;i<sz;i++) | |
p[i]=str.p[i]; | |
return *this; | |
} | |
/****************************************************** | |
Function: replace | |
Arguments: const size_t& pos,const size_t& n,const char* cs | |
Description: This method is used to replace a string. | |
Return: String& | |
*******************************************************/ | |
String& String::replace(const size_t& pos,const size_t& n,const char* cs) | |
{ | |
unsigned i; | |
String str(p); | |
String str1(cs); | |
str.replace(pos,n,str1); | |
sz=str.sz; | |
delete [] p; | |
p=new char[sz]; | |
for(i=0;i<sz;i++) | |
p[i]=str.p[i]; | |
return *this; | |
} | |
/****************************************************** | |
Function: replace | |
Arguments: const size_t& pos1,const size_t& n1,const String& s,const size_t& pos2,const size_t& n2 | |
Description: This method is used to replace a string. | |
Return: String& | |
*******************************************************/ | |
String& String::replace(const size_t& pos1,const size_t& n1,const String& s,const size_t& pos2,const size_t& n2) | |
{ | |
unsigned i; | |
String str(p); | |
String str1(s,pos2,n2); | |
str.erase(pos1,n1); | |
str.insert(pos1,str1); | |
sz=str.sz; | |
delete [] p; | |
p=new char[sz]; | |
for(i=0;i<sz;i++) | |
p[i]=str.p[i]; | |
return *this; | |
} | |
/****************************************************** | |
Function: replace | |
Arguments: const size_t& pos1,const size_t& n1,const String& s,const size_t& pos2,const size_t& n2 | |
Description: This method is used to replace a string. | |
Return: String& | |
*******************************************************/ | |
String& String::replace(const size_t& pos,const size_t& n1,const char* cs,const size_t& n2) | |
{ | |
unsigned i; | |
String str(p); | |
String str1(cs,n2); | |
str.erase(pos,n1); | |
str.insert(pos,str1); | |
sz=str.sz; | |
delete [] p; | |
p=new char[sz]; | |
for(i=0;i<sz;i++) | |
p[i]=str.p[i]; | |
return *this; | |
} | |
/****************************************************** | |
Function: replace | |
Arguments: const size_t& pos,const size_t& n1,const size_t& n2,const char& c | |
Description: This method is used to replace a string. | |
Return: String& | |
*******************************************************/ | |
String& String::replace(const size_t& pos,const size_t& n1,const size_t& n2,const char& c) | |
{ | |
unsigned i; | |
String str(p); | |
String str1(n2,c); | |
str.replace(pos,n1,str1); | |
sz=str.sz; | |
delete [] p; | |
p=new char[sz]; | |
for(i=0;i<sz;i++) | |
p[i]=str.p[i]; | |
return *this; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment