Question
IMPLEMENT the Following instructions into the String.hpp and String.cpp provided. DO NOT just simply copy the code I give you as your answer Intructions: Implementation:
IMPLEMENT the Following instructions into the String.hpp and String.cpp provided. DO NOT just simply copy the code I give you as your answer
Intructions:
Implementation:
For your String class:
1) Implement method String String::substr(int start_pos, int count) const;. This will return the specified substring starting at start_pos and consisting of count characters (there will be less than count if it extends past the end of the string).
2) Implement method int String::find(char ch, int start_pos) const; which gives the first location starting at start_pos of the character ch or -1 if not found.
3) Implement method int String::find(const String & s, int start_pos) const; which gives the first location starting at start_pos of the substring s or -1 if not found.
4) Implement a method std::vector
5) You will use std::vector<> (need to include
This method will return a vector of String split up based on a supplied character. For example given s = "abc ef gh", the call s.split(' ') will return a vector with three strings: "abc", "ef", and "gh".
std::vector has a number of operations defined including operator[], and size (number of elements in the vector).
-------------------------------------------------------------------------------------
string.hpp
-------------------------------------------------------------------------------------
#ifndef STRING_HPP #define STRING_HPP #include/** * @invariant str[length()] == 0 * && length() == capacity() * && capacity() == stringSize - 1 */ class String { private: // helper constructors and methods String(int); String(int, const char *); void reset_capacity (int); char * str; // size includes NULL terminator int string_size; public: // constructor: empty string, String('x'), and String("abcd") String(); String(char); String(const char *); // copy ctor, destructor, constant time swap, and assignment String(const String &); ~String(); void swap (String &); String & operator= (String); // subscript: accessor/modifier and accessor char & operator[](int); char operator[](int) const; // max chars that can be stored (not including null terminator) int capacity() const; // number of char in string int length () const; // concatenation String operator+ (const String &) const; String & operator+=(String); // relational methods bool operator==(const String &) const; bool operator< (const String &) const; // i/o friend std::ostream& operator<<(std::ostream &, const String &); friend std::istream& operator>>(std::istream &, String &); }; // free functios for concatenation and relational String operator+ (const char *, const String &); String operator+ (char, const String &); bool operator== (const char *, const String &); bool operator== (char, const String &); bool operator< (const char *, const String &); bool operator< (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 &); #endif
-------------------------------------------------------------------------------
string.cpp
-------------------------------------------------------------------------------
#include#include #include #include "string.hpp" String::String(int n){ string_size = n; str = new char[n]; str[0] = '\0'; } String::~String(){ delete [] str; } String::String(int x, const char *str1){ string_size = x; str = new char[x]; int pos = 0; while(str1[pos] != '\0') { str[pos] = str1[pos]; ++pos; } str[pos] = '\0'; } void String::reset_capacity(int x){ String str1(x, str); swap(str1); } String::String(){ str = new char[1]; str[0] = '\0'; string_size = 1; } String::String(char ch){ string_size = 2; str = new char[string_size]; str[0] = ch; str[1] = '\0'; } String::String(const char* s){ int counter = 0; while(s[counter] != '\0'){ ++counter; } string_size = counter + 1; str = new char[string_size]; for(int i = 0; i < counter ; i++){ str[i] = s[i]; } str[counter] = '\0'; } String::String(const String& s){ string_size = s.string_size; str = new char[string_size]; int pos = 0; for(int i = 0; i < string_size; ++i){ str[i] = s.str[i]; ++pos; } } int String::length() const{ int size = 0; while(str[size] != '\0') ++size; return size; } int String::capacity() const{ return string_size - 1; } String& String::operator=(String str1){ string_size = str1.string_size; for(int i = 0; i < string_size; i++){ str[i] = str1.str[i]; } return *this; } char& String::operator[](int i){ assert(i >= 0); assert(i <= length()); return str[i]; } void String::swap(String& str1){ int tempStr = string_size; string_size = str1.string_size; str1.string_size = tempStr; char* tempStr1 = str; str = str1.str; str1.str = tempStr1; } char String::operator[](int i) const{ assert(i >= 0); assert(i <= length()); return str[i]; } bool String::operator==(const String& rhs) const{ int pos = 0; while(str[pos] != '\0' && str[pos] == rhs.str[pos]){ ++pos; } return str[pos] == rhs.str[pos]; } std::istream& operator>>(std::istream& in, String& rhs){ in >> rhs.str; return in; } std::ostream& operator<<(std::ostream& out, const String& rhs){ out << rhs.str; return out; } bool String::operator<(const String& rhs) const{ int pos = 0; while(str[pos] != '\0' && rhs.str[pos] != '\0' && str[pos] == rhs.str[pos]){ ++pos; } return str[pos] < rhs.str[pos]; } String String::operator+(const String& rhs) const{ int offset2 = (length() + rhs.length()) + 1; String result(offset2); int offset = length(); int count = 0; int pos = 0; while(str[count] != '\0'){ result.str[count] = str[count]; ++count; } while(rhs.str[pos] != '\0'){ result.str[offset + pos] = rhs.str[pos]; ++pos; } result.str[offset2 - 1] = '\0'; return result; } String& String::operator+=(String rhs){ int offset = length(); int pos = 0; int newSize = (length() + rhs.length()) + 1; String result(newSize); reset_capacity(newSize); for(int x = offset; x < newSize; ++x){ str[x] = rhs.str[pos]; ++pos; } str[newSize - 1] = '\0'; return *this; } String operator+(const char charArray[], const String& rhs){ String s(charArray); return rhs + s; } String operator+(char s, const String& rhs){ return s + rhs; } bool operator==(const char charArray[], const String& rhs){ if(charArray == rhs){ return true; } else{ return false; } } bool operator==(char s, const String& rhs){ if(s == rhs){ return true; } else{ return false; } } bool operator<(const char charArray[], const String& rhs){ if(charArray < rhs){ return true; } else{ return false; } } bool operator<(char s, const String& rhs){ if(s < rhs){ return true; } else{ return false; } } bool operator<=(const String& lhs, const String& rhs){ if(lhs < rhs || lhs == rhs){ return true; } else{ return false; } } bool operator!=(const String& lhs, const String& rhs){ if(lhs.length() != rhs.length()){ return true; } int pos = 0; while(lhs[pos] != rhs[pos]){ pos++; } if(pos == lhs.length()){ return true; } return false; } bool operator>=(const String& lhs, const String& rhs){ if(lhs > rhs || lhs == rhs) { return true; } else{ return false; } } bool operator>(const String& lhs, const String& rhs){ if(!(lhs <= rhs)){ return true; } else{ return false; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started