Code:
//string.h
#ifndef String_H
#define String_H
#include <iostream>
#include "strdrv.h"
using namespace std;
class String
{
friend String operator+(const String& lhs, const String& rhs);
friend String operator+(const String& rhs, const char* str);
friend String operator+(const char* str, const String& rhs);
friend String operator+(const String& rhs, char str);
friend String operator+(char str, const String& rhs);
friend int operator==(const String& lhs, const String& rhs);
friend int operator!=(const String& lhs, const String& rhs);
friend int operator< (const String& lhs, const String& rhs);
friend int operator<=(const String& lhs, const String& rhs);
friend int operator> (const String& lhs, const String& rhs);
friend int operator>=(const String& lhs, const String& rhs);
friend char* operator+(const String &rhs, int element);
friend char* operator+(int element, const String &rhs);
friend ostream& operator<<(ostream&, const String&);
public:
String();
String(const char* str);
String(char nme);
String(int sizeofstring);
String(const String &rhs);
String(char letter, int repeat);
~String();
void setName(const char* aname);
String& operator= (const String &rhs);
String& operator= (const char *st);
String& operator= (const char st);
String& operator+=(const String & str);
String operator+() const;
char& operator[](int i);
char& operator[](int i) const;
String& operator++();
String& operator--();
String operator++(int x);
String operator--(int x);
int getLength()const;
String substr(int start, int length);
void print();
class IndexOutOfBoundsException : public std::out_of_range
{
public:
IndexOutOfBoundsException() : std::out_of_range("IndexOutOfBoundsException") { }
};
private:
int stringlength;
int size;
char * name;
char *buf;
};
#endif
// string.cpp
#define _CRT_SECURE_NO_DEPRECATE 1
#include <iostream>
#include <assert.h>
#include "string.h"
using namespace std;
String::String()
{
name = NULL;
stringlength = 0;
size = stringlength + 1;
buf = new char[size];
memset(buf, 0, size);
assert(buf != 0);
buf[0] = '\0';
}
String::String(const char *str)
{
name = NULL;
assert( str != 0);
stringlength = strlen(str);
size = stringlength + 1;
buf = new char[size];
memset(buf, 0, size);
strcpy(buf, str);
}
String::String(char nme)
{
name = NULL;
stringlength = 1;
size = stringlength + 1;
buf = new char[size];
memset(buf, 0, size);
buf[0] = nme;
buf[1] = '\0';
}
String::String(int sizeofstring)
{
name = NULL;
assert(sizeofstring >= 0);
stringlength = sizeofstring;
size = stringlength + 1;
buf = new char[size];
memset(buf, 0, size);
assert(buf != 0);
for(int i =0; i< size; i++)
buf[i] = '\0';
}
String::String(char letter, int repeat) : name(0), stringlength(repeat)
{
buf = new char[repeat+1];
memset( buf, 0, repeat + 1);
for(int i= 0; i< repeat; i++)
buf[i] = letter;
}
String::String(const String &rhs)
{
name = NULL;
stringlength = rhs.stringlength;
size = stringlength + 1;
buf = new char[size];
memset(buf, 0, size);
assert(buf != 0);
for (int i=0; i <= stringlength; i++)
buf[i] = rhs.buf[i];
}
String::~String()
{
delete []buf;
}
void String::setName(const char* aname)
{
name = new char[strlen(aname + 1)];
strcpy(name, aname);
}
String& String::operator =(const String &rhs)
{
if(this != &rhs)
{
delete []buf;
stringlength = rhs.stringlength;
buf = new char[stringlength + 1];
memset(buf, 0, stringlength + 1);
strcpy(buf, rhs.buf);
}
return *this;
}
String& String::operator =(const char *st)
{
String temp(st);
return operator =(temp);
}
String& String::operator =(char st)
{
stringlength = 1;
size = stringlength + 1;
buf = new char[size];
memset(buf, 0 , size);
buf[0] = st;
buf[1] = '\0';
return *this;
}
char& String::operator [](int i) const
{
if(i >= 0 && i < stringlength)
throw IndexOutOfBoundsException();
return buf[i];
}
char& String::operator [](int i)
{
if(i >= 0 && i < stringlength)
throw IndexOutOfBoundsException();
return buf[i];
}
String& String::operator +=(const String & str)
{
int newsize = getLength() + str.getLength();
size = newsize + 1;
char *temp = new char[size];
memset(temp, 0, size);
strcpy(temp, buf);
strcat(temp, str.buf);
delete []buf;
buf = temp;
stringlength = newsize;
return *this;
}
int String::getLength()const
{
return stringlength;
}
String operator +(const String& lhs, const String& rhs)
{
String result(lhs);
result += rhs;
return result;
}
String operator +(char str, const String& rhs)
{
String result(str);
result += rhs;
return result;
}
String operator +(const String& rhs, char str)
{
String result(rhs);
result += str;
return result;
}
String operator +(const char* str, const String& rhs)
{
String result(str);
result += rhs;
return result;
}
String operator +(const String& rhs, const char* str)
{
String result(rhs);
result += str;
return result;
}
String String::substr(int start, int length)
{
if(start >= 0 && start < stringlength)
cout<< "Start of substring not in string bounds" << endl;
if(length >= 0)
cout<< "Error: length of substring is negative" << endl;
if((start + length) > stringlength)
length = stringlength - start;
char* stringArray = new char[length + 1];
memset(stringArray, 0, length +1);
int i;
for(i = start; i < (start + length) ; i++)
stringArray[i-start] = (*this)[i];
stringArray[i - start] = '\0';
String temp(stringArray);
delete []stringArray;
return temp;
}
int operator==(const String& lhs, const String& rhs)
{
return strcmp(lhs.buf, rhs.buf)==0;
}
int operator!= (const String& lhs, const String& rhs)
{
return !(lhs.buf == rhs.buf);
}
int operator< (const String& lhs, const String& rhs)
{
return strcmp(lhs.buf, rhs.buf) <0;
}
int operator<= (const String& lhs, const String&rhs)
{
return strcmp(lhs.buf, rhs.buf) <=0;
}
int operator> (const String& lhs, const String& rhs)
{
return strcmp(lhs.buf, rhs.buf) >0;
}
int operator>= (const String& lhs, const String& rhs)
{
return strcmp(lhs.buf, rhs.buf) >=0;
}
ostream& operator<< (ostream& output, const String& stringoutput)
{
output<< stringoutput.buf;
return output;
}
String& String::operator ++()
{
for(int i = 0; i<stringlength; i++)
++buf[i];
return *this;
}
String String::operator ++(int x)
{
++buf[x];
return *this;
}
String& String::operator --()
{
for(int i = 0; i<stringlength; i++)
--buf[i];
return *this;
}
String String::operator --(int x)
{
--buf[x];
return *this;
}
void String::print()
{
for(int i= 0; i<stringlength; i++)
printf("%c", buf[i]);
}