Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Venix Cador
- //String ADT
- //Cs 23001
- #include "string.h"
- String::String(const int cap){
- size = cap;
- s= new char[size];
- s[0] = 0;
- length = 0;
- }
- String::String(const char c, const int cap){
- size = cap;
- s = new char[size];
- s[0] = c;
- s[1] = 0;
- length = 1;//because the length is one
- }
- //copy constructor
- String::String(const String& rhs, const int cap)
- {
- //check if capacity is shorter than length
- if(cap <= rhs.length){
- size = rhs.length + 1;
- }else{
- size = cap;
- }
- s = new char[size];
- //copy chars to string
- for(length = 0; rhs.s[length] != 0; ++length){
- s[length] = rhs.s[length];
- }
- s[length] = 0;
- }
- String::String(const char str[], const int cap){
- //calculate the length of the string
- for (length = 0; str[length] != 0; ++length){}
- //resize the size is needed
- if(length > cap - 1) {
- size = length + 1;
- }else {
- size = cap;
- }
- s = new char[size];
- //copy chars to string
- for(int index = 0; str[index] != 0; ++index){
- s[index] = str[index];
- }
- s[length] = 0;
- }
- //destructor for string
- String::~String()
- {
- length = 0;
- delete [] s;
- }
- //finds the first occurance of a char in a string with zero offset
- //Ex: str.finchar('d');
- int String::findchar(char find) const
- {
- for(int i = 0; i < length; ++i){
- if(s[i] == find){
- return i;
- }
- }
- return -1;
- }
- //finds how many times a string occurs in another string
- //Ex: str.findstr(find);
- int String::findstr(const String& find) const
- {
- int result = 0;
- if( length > find.length){
- for(int index = 0, findindex = 0; index < length; ++index, findindex = 0){
- while ( s[index + findindex] == find.s[findindex] && findindex <= find.length){
- ++findindex;
- if (findindex == find.length) {
- ++result;
- index += findindex;
- }
- }
- }
- }
- return result;
- }
- bool String::operator==(const String& rhs) const {
- if(length != rhs.length) return false;
- for(int i = 0; i < length; ++i){
- if(s[i] != rhs.s[i]) return false;
- }
- return true;
- }
- /*
- *Returns the character from a specified index. Returns null if it is out of bounds.
- *Ex: char d = str[1];
- */
- char String::operator [] (int index) const{
- if( index >= length || index < 0) {
- std::exit(0);
- }
- return s[index];
- }
- char& String::operator [] (int index){
- if(index >= length || index < 0){
- std:: exit(0);
- }
- return s[index];
- }
- String String::operator + (const String& rhs) const {
- int index = 0;
- String result;
- result.length = length + rhs.length;
- // Taking the index and goes to the string in order to find the length of the first string
- for(; index <length; ++index)
- {
- result.s[index] = s[index];
- }
- for(int rhsindex = 0; rhsindex < rhs.length; ++rhsindex, ++index)
- {
- result.s[index] = rhs.s[rhsindex];
- }
- result.s[index] = 0;
- return result;
- }
- //greater than operator this is fliped compare to the less than operator
- bool String::operator < (const String& rhs) const
- {
- int i = 0;
- while((s[i] != 0) && (rhs.s[i] != 0) && (i< MaxString)) {
- if (s[i] < rhs.s[i]) return true;
- if(s[i] == rhs.s[i]) ++i;
- else return false;
- }
- if(rhs.s[i] != 0)
- return false;
- return true;
- }
- // the output function
- std::ostream& operator <<(std::ostream& out, const String& rhs){
- int i = 0;
- while(rhs[i] != 0){
- out<< rhs[i];
- ++i;
- }
- return out;
- }
- //The input function
- std::istream& operator >>(std::istream& in, String& rhs){
- char tmp;
- while (in) {
- in.get(tmp);
- if (!in.eof()) {
- rhs += tmp;
- }
- }
- return in;
- }
- //the substr function
- String String::substr(int left, int right) const
- {
- String result;
- if (right <= 0 || right > length){
- right = length;
- }
- for(int i = left; i < right; ++i){
- result = result + s[i];
- }
- result.length = right - left;
- result[right] = 0;
- return result;
- }
- //Swaps two strings
- //Ex: str1.swap(str2);
- //reallocate string's capacity to a specified value
- //Ex: str.reallpcate(50);
- void String::reallocate(const int cap)
- {
- String temp(*this, cap);
- swap(temp);
- }
- //swap two strings
- //Ex: str1.swap(str2)
- void String::swap(String& str)
- {
- char *temp = s;
- s = str.s;
- str.s = temp;
- int temp_cap = size;
- size = str.size;
- str.size = temp_cap;
- int temp_length = length;
- length = str.length;
- str.length = temp_length;
- }
- //assignment operator for string
- //Ex: String str = rhs_str;
- String& String::operator = (String rhs)
- {
- swap(rhs);
- return *this;
- }
Advertisement
Add Comment
Please, Sign In to add comment