Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- const int DEFAULT_STRING_CAPACITY = 256;
- class String {
- public:
- String ();
- String (const char);
- String (const char*);
- String (const int, const char*);
- String (int);
- String (const String&); // Copy constructor
- ~String (); // Destructor
- String& operator= (String); // Assignment operator
- void resetCapacity (int);
- void swap (String&);
- char& operator[] (int);
- char operator[] (int) const;
- int capacity () const;
- int length () const;
- bool operator== (const String&) const;
- bool operator< (const String&) const;
- String operator+ (const String&) const;
- String substr (int, int) const;
- int findstr (const String&) const;
- int findchar (const char) const;
- int nextBlank (const int) const;
- int nextNonBlank (const int) const;
- String justify (const int) const;
- friend std::ostream& operator<<(std::ostream&, const String&);
- friend std::istream& operator>>(std::istream&, String&);
- private:
- char *S;
- int cap;
- };
- String operator+ (const char[], const String&);
- String operator+ (const char, const String&);
- bool operator== (const char[], const String&);
- bool operator== (const char, const String&);
- bool operator< (const char[], const String&);
- bool operator< (const char, const String&);
- bool operator<= (const String&, const String&);
- bool operator!= (const String&, const String&);
- bool operator>= (const String&, const String&);
- bool operator> (const String&, const String&);
- #include "string.hpp"
- #include<cassert>
- String::String()
- {
- cap = DEFAULT_STRING_CAPACITY;
- S = new char[0];
- }
- String::String(const char Ch)
- {
- cap = DEFAULT_STRING_CAPACITY;
- S = new char[cap];
- S[0] = Ch;
- S[1] = '\0'; //NULL Term. char
- }
- String::String(const char *Temp)
- {
- cap = DEFAULT_STRING_CAPACITY;
- S = new char[cap];
- int i = 0;
- while(Temp[i] != '\0')
- {
- S[i] = Temp[i];
- ++i;
- }
- S[i] = '\0'; //NULL terminating
- }
- String::String(int Num)
- {
- cap = Num;
- S = new char[cap];
- }
- String::String(const int Num, const char *Temp)
- {
- cap = Num;
- S = new char[cap];
- int i = 0;
- while(Temp[i] != 0 && i < cap)
- {
- S[i] = Temp[i];
- ++i;
- }
- S[i] = '\0';
- }
- String::String(const String& Actual)
- {
- cap = Actual.cap;
- S = new char[cap];
- for(int i = 0; i < cap; ++i)
- S[i] = Actual.S[i];
- }
- String::~String()
- {
- delete[] S;
- }
- String& String::operator=(String Rhs)
- {
- if(S == Rhs.S)
- return *this;
- delete[] S;
- cap = Rhs.cap;
- S = new char[cap];
- for(int i = 0; i < cap; ++i)
- S[i] = Rhs.S[i];
- //swap(Rhs);
- return *this;
- }
- char& String::operator[](int Num)
- {
- if(Num < 0 || Num >= length())
- return S[Num] = 0;
- return S[Num];
- }
- char String::operator[](int Num) const
- {
- if(Num >= 0 && Num < length())
- return S[Num];
- else
- return 0;
- }
- int String::capacity() const
- {
- return cap;
- }
- int String::length() const
- {
- int result = 0;
- while(S[result] != '\0')
- ++result;
- return result;
- }
- bool String::operator==(const String& Rhs) const
- {
- int i = 0;
- while( (S[i] != 0) && (Rhs[i] != 0) && (S[i] == Rhs[i]) )
- ++i;
- if(S[i] == Rhs[i])
- return true;
- return false;
- }
- bool String::operator<(const String& Rhs) const
- {
- int i = 0;
- while( (S[i] != 0) && (Rhs.S[i] != 0) && (S[i] == Rhs[i]) )
- ++i;
- if( (S[i] == 0) && (Rhs[i] == 0) )
- return false;
- if(S[i] < Rhs.S[i])
- return true;
- if(S[i] == 0)
- return true;
- return false;
- }
- String String::operator+(const String& Rhs) const
- {
- String result(S);
- int offset = length();
- int i = 0;
- while(Rhs.S[i] != 0)
- {
- result.S[offset + i] = Rhs.S[i];
- ++i;
- }
- result.S[offset + i] = '\0';
- return result;
- }
- std::ostream& operator<<(std::ostream& Out, const String& Rhs)
- {
- Out << Rhs.S;
- return Out;
- }
- std::istream& operator>>(std::istream& In, String& Rhs)
- {
- char buf[DEFAULT_STRING_CAPACITY];
- In >> buf;
- Rhs = String(buf);
- return In;
- }
- String operator+(const char* Lhs, const String& Rhs)
- {
- String result = Lhs;
- result = result + Rhs;
- return result;
- }
- String operator+(const char Lhs, const String& Rhs)
- {
- String result = Lhs;
- result = result + Rhs;
- return result;
- }
- bool operator==(const char* Lhs, const String& Rhs)
- {
- String result = Lhs;
- if(result == Rhs)
- return true;
- return false;
- }
- bool operator==(const char Lhs, const String& Rhs)
- {
- String result = Lhs;
- if(result == Rhs)
- return true;
- return false;
- }
- bool operator<(const char* Lhs, const String& Rhs)
- {
- String result = Lhs;
- if(result < Rhs)
- return true;
- return false;
- }
- bool operator<(const char Lhs, const String& Rhs)
- {
- String result = Lhs;
- if(result < Rhs)
- return true;
- return false;
- }
- bool operator<=(const String& Lhs, const String& Rhs)
- {
- if(Lhs < Rhs || Lhs == Rhs)
- return true;
- return false;
- }
- bool operator!=(const String& Lhs, const String& Rhs)
- {
- if(!(Lhs.operator==(Rhs)) )
- return true;
- return false;
- }
- bool operator>=(const String& Lhs, const String& Rhs)
- {
- if(!(Lhs.operator<(Rhs)) || (Lhs == Rhs) )
- return true;
- return false;
- }
- bool operator>(const String& Lhs, const String& Rhs)
- {
- if(!(Lhs.operator<(Rhs)) && (Lhs != Rhs) )
- return true;
- return false;
- }
- String String::substr(int Start, int End) const
- {
- String result;
- assert(End <= length());
- for(int i = Start; i <= End; ++i)
- result = result + S[i];
- return result;
- }
- // Finds fisrt match in string
- // Returns -1 if no match
- int String::findstr(const String& Search) const
- {
- int count = 0;
- int start = 0;
- int i = 0;
- while(i <= length())
- {
- if(S[i] == Search.S[count])
- {
- ++count;
- start = i;
- }
- ++i;
- if(count == Search.length())
- return start - (count - 1);
- }
- if(count != Search.length())
- return -1;
- return -1;
- }
- int String::findchar(const char Search) const
- {
- int i = 0;
- while(i <= length())
- {
- if(S[i] == Search)
- {
- return i;
- }
- else if(i == length())
- return -1;
- ++i;
- }
- return -1;
- }
- void String::resetCapacity(int Newcap)
- {
- char *temp = new char[Newcap];
- int smallest = cap;
- if(cap > Newcap)
- smallest = Newcap;
- for(int i = 0; i < smallest; ++i)
- temp[i] = S[i];
- delete[] S;
- S = temp;
- cap = Newcap;
- }
- void String::swap(String& Rhs)
- {
- char *strtmp = S;
- S = Rhs.S;
- Rhs.S = strtmp;
- int captmp = cap;
- cap = Rhs.cap;
- Rhs.cap = captmp;
- }
- int String::nextBlank(const int Index) const
- {
- int i = Index;
- while(i <= length())
- {
- if(S[i] == ' ' && (i != Index || S[0] == ' '))
- {
- return i;
- }
- else if(i == length())
- return -1;
- ++i;
- }
- return -1;
- }
- int String::nextNonBlank(const int Index) const
- {
- int i = Index;
- while(i <= length())
- {
- if(S[i] != ' ' && i != Index )
- {
- return i;
- }
- ++i;
- }
- return -1;
- }
- String String::justify(const int Width) const
- {
- String result = *this;
- int i = 0;
- if(nextBlank(0) == -1 || length() > Width)
- return *this;
- while(result.length() < Width)
- {
- i = result.nextBlank(i);
- result = result.substr(0,i) + " " +result.substr(i + 1, result.length());
- i = result.nextNonBlank(i);
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement