In: Computer Science
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:
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).
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.
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.
Implement a method std::vector<String> String::split(char) const;
You will use std::vector<> (need to include <vector>) for storing the results of parsing the input data.
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 <iostream>
/**
 * @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 <cstring>
#include <iostream>
#include <cassert>
#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;
  }
}
Here are the functions for you:
String String::substr(int start_pos, int count) const{
char s[count+1];
int i, pos=0;
for(int i = start_pos; (i < string_size-1) && (pos <
count); i++){
s[pos++] = str[i];
}
s[pos] = '\0';
return String(s);
}
int String::find(char ch, int start_pos) const{
int i=start_pos;
int index = -1;
for(int i = start_pos; (i < string_size-1); i++){
if(str[i] == ch)
return i;
}
return index;
}
int String::find(const String & s, int start_pos) const
{
  
int i=start_pos;
int len = s.string_size-1;
int index = -1;
  
for(int i = start_pos; (i < string_size-1); i++){
bool found = true;
for(int j=0; ((j+i) < string_size-1) && j < len; j++)
{
if(str[i+j] != s.str[j]) {
found = false;
break;
}
}
if(found) {
return i;
}
}
  
return index;
}
std::vector<String> String::split(char ch) const{
std::vector<String> v;
int start = 0;
for(int i=0; i< string_size-1; i++) {
// if character matches, provide the string from start to
this
// position and then move start to i+1
if(str[i] == ch) {
if(i > start) {
v.push_back(substr(start, i-start));
start = i+1;
}
}
}
// push the remaining string
if(start < string_size-1) {
v.push_back(substr(start, string_size-1));
}
  
return v;
}