Question

In: Computer Science

IMPLEMENT the Following instructions into the String.hpp and String.cpp provided. DO NOT just simply copy the...

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;
  }
}

Solutions

Expert Solution

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;
}


Related Solutions

In C++, Implement the heapafication operation. Do not re-implement the binary tree class. Simply create a...
In C++, Implement the heapafication operation. Do not re-implement the binary tree class. Simply create a funcntion that takes in a binary tree and heapafies it. Meaning it takes in a pointer to a binary tree and converts it into a heap. (You may choose max or min heap).
(USING YOUR OWN WORDS!!! DO NOT COPY OFF THE INTERNET) Instructions: Answer any ONE of the...
(USING YOUR OWN WORDS!!! DO NOT COPY OFF THE INTERNET) Instructions: Answer any ONE of the following essay questions. Your response should be in essay format. Write as much as possible telling me who, what, where, when, and why. Use complete sentences and multiple paragraphs; 3-5-7 total. Your response is worth up to 20 points. Discuss the Holocaust. What was it and where and when did it take place? What were the reasons for such a thing? Who were the...
Video Response 8.1 - "Search and Seizure" Instructions please do not copy and paste from internet-answer...
Video Response 8.1 - "Search and Seizure" Instructions please do not copy and paste from internet-answer based on the video-thanky ou Write a 200- to 400-word essay about the most important thing you learned while watching "Search and Seizure." video: you can find it easily by typing: Search and Seizure: Crash Course Government and Politics #27 or link if needed: https://www.youtube.com/watch?v=_4O1OlGyTuU&list=PL8dPuuaLjXtOfse2ncvffeelTrqvhrz8H&index=27
Use the correlation section of the PDF instructions provided. Do a complete and thorough write up...
Use the correlation section of the PDF instructions provided. Do a complete and thorough write up on the following correlation analysis. The variable Stay represents the average length of stay in days at a sample of hospitals across the country. Age is the average age of patients. Culture is a measure of the level of understanding of the importance of each employee in helping patients get well. Correlations Stay Age Culture Stay Pearson Correlation 1 .189* .327** Sig. (2-tailed) .045...
Write the MIPS assembly codes to implement the following function: copy a block of words from...
Write the MIPS assembly codes to implement the following function: copy a block of words from one address to another. Assume that the starting address of the source block be in register $t1 and that the destination address be in $t2. The instruction also requires that the number of words to copy in $t3 (which is >0, that means how many words are needed to copy). Furthermore, assume that the values of these registers as well as register $t4 can...
Choose an occupational health hazard interest to you. *********************** Do not copy write or just post...
Choose an occupational health hazard interest to you. *********************** Do not copy write or just post something from google, you will get a thumbs down!!! please and thank you ***** do not hand write please As a chemical engineer professional, if you were to encounter this hazard in a work environment or situation (thoroughly identify the hazard and describe the situation) describe how you would control the hazard for the workers working in the environment or situation you presented.
NEED DIAGRAM! DO NOT JUST COPY THE ANSWER FROM THE COURSEHERO!! THX Mr. and Mrs. Brauer...
NEED DIAGRAM! DO NOT JUST COPY THE ANSWER FROM THE COURSEHERO!! THX Mr. and Mrs. Brauer owned their own home. There was a real estate boom in their town and the price of houses doubled. Their income and prices of other goods stayed constant. The Brauers complained that “we are being driven from our home; we can’t afford to live here any more.” Assume the Brauers has two consumption goods: “housing” and “other goods”. a. Draw a diagram that illustrates...
How do you implement stack by using linked list? No code just explain it.
How do you implement stack by using linked list? No code just explain it.
You are provided with a StackInterface to implement your stack, make sure to implement the data...
You are provided with a StackInterface to implement your stack, make sure to implement the data structure using java.util.ArrayList and naming your implementation ArrayListStack (make sure to use generics like you have been for the data structures so far). The provided StackTester assumes you name your implementation that way. Note that you have also been provided a PalindromeTester with the specific isPalindrome() method you are required to implement, following the approach above. The method should take whitespace, case-sensitivity, digits, and...
1)You are instructed to reflux the reaction for 1 hour. If the instructions simply told you...
1)You are instructed to reflux the reaction for 1 hour. If the instructions simply told you to reflux until the reaction was complete, how would you know when this was?Questions regarding Organic Chemistry Lab: Fischer Esterification 2)Why do we wash the organic with aqueous base? Is acetic acid water soluble (how do you know)? If it is water soluble, why don’t we just wash with water instead of aqueous base? 3)In most cases of Fischer esterification, the carboxylic acid is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT