QuestionQuestion

Strings are Easy
In this assignment you need to write a String Class. That’s it! Piece of cake.

You can only use dynamic arrays of characters for this assignment. No strings! No strcpy!
Your string class should allow for the following operations. You will overload the operators accordingly. To get full points you have to overload the following operators to do as follows

Sample Code: StringA= “Potatoe”; String B = “are”; String C = “Easy”; String D;
String E = “otat”;
+ (Concatenation):
Grammar:
S::= A
A ::= B | ‘+’ A
B ::= <string> A | ε
S => A => B => <string> A => <string> ‘+’ A => <sting> ‘+’ B => <string> ‘+’ <string> A => <string> ‘+’ <string> B => <string> ‘+’ <string> ε
<string> ‘+’ <string>
D = C +A

Should store in D = “EasyPotatoe”

It should work the same way concatenation works with C++’s normal string datatype
- (Subtraction):
D = A - E;
Should store in D = “Poe”

Subtraction should remove the first instance (left to right) of that combination of letters. So if we had “Banana” and we are subtracting “ana” from it the resulting string would be “Bna” and NOT “Ban”. Another example if we had “Banana” and we were subtracting “a” from it we would get “Bnana”. Note that we will not deal with negative strings so if given A – E , If E > A then return A.
* (Multiplication):
<string>=<string>‘*’<integer> | <integer>‘*’<string> If C = “Easy”
D = C * 3;
D = EasyEasyEasy

Duplicate the string by the given integer that many times.
/ (Division):
String* result = <string> ‘/’ <integer>

Division should split the given string into equal parts (character count wise) and discard the remainder for strings that cannot be split evenly. (This remainder is what the modulus operator will return). The split strings should be returned in an array of type String

If A = Potatoe
A / 3 will return an array with 3 members and evenly divide the string into such, so

A[0] = Po
A[1] = ta
A[2] = to

Another example, A / 4 would return A[0] = P
A[1] = o
A[2] = t
A[3] = a
A / 1 would return the original word. A / 0 would return the empty string.

For the output file, just print the entire array with one member per line. Unlike the other parts of this assignment you will not have to combine this operation with any other unless it is the last part of it.
% (Modulus):
<string> =

As mention in division. The remainder you get when splitting the string will be what this outputs. So for A /3 it would return the “e” in “potatoe”. A/3 would return “toe”
SIZE Function:

Should return the number of characters in a string, and if your string is empty it should return 0. Recall that an empty string is represented by “”.
<< / >> (Insertion and Extraction Operators):

Overload these so you can use cin and cout with your program (print functions etc). Tip: Use cin.peek()

Note: Do not follow Operator Precedence rules. Merely work your way from left to right and make sure you can do multiple operations in one line. So: Car + men * 2 should return CarmenCarmen

All in all your program should dynamically allocate arrays of CHARACTERs. Remember do not use the string functions. I repeat, YOU CANNOT USE THE C++ STRING DATATYPE. AGAIN, C++ STRING DATATYPE SHALL NO PASS. Including the available functions for it. If you do you will lose 50% of the points.

Notes:
-YOU CANNOT USE THE C++ STRING DATATYPE. Including the available functions for it. If you do you will lose 50% of the points.
-Comment your source code appropriately.
-For any operation where you return nothing, such as modulus with an even split. Return the empty string “”.
-Make sure you name your program file in accordance with the syllabus stipulations
-Test your program by running it until it works properly with different input to make sure the output is correct.

Test and execute your program by using data-in by Linux input redirection. If your executable code is Ast# and your data file is named data# execute as: Ast# < data#

Your input will consist of operations you must deal with and output for each line the resulting operation.

Sample Input:
Banana - ana
Banana - ana + bana * 3 - na
Potatoe / 3
Potatoe % 3
Hear + me / 2
Sample output:
Bna
BbanaBnabanaBnabana
Po
ta
to
e
Hea
rMe

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#include <iostream>
using namespace std;

class String {
private:
char* data;
public:
// Constructor
String(const char* chars = "") {
    // Find the size of the input
    int size = 0;
    while(chars[size] != '\0') {
      size++;
    }
    // Allocate enough memory for our data
    // Take '\0' (last character) into account
    data = new char[size+1];
    for(int i=0; i<size; i++) {
      data[i] = chars[i];
    }
    data[size] = '\0';
}

// Copy constructor
String(const String &other) {
    int size = other.size();
    // Allocate enough memory for our data
    // Take '\0' (last character) into account
    data = new char[size+1];
    for(int i=0; i<size+1; i++) {
      data[i] = other.data[i];
    }
}

// Destructor
~String() {
    // Free allocated memory
    delete [] data;
}

// Size function
int size() const {
    int cnt = 0;
    while(data[cnt] != '\0') {
      cnt++;
    }
    return cnt;
}

// Assignment operator
String& operator=(const String &other) {
    // If it is self-assignment, just return itself
    if(this == &other) {
      return *this;
    }
    // Else, free all the memory and reallocate it for the new data
    else {
      // Free memory
      delete [] data;
      // Allocate new memory of the size of the second string
      data = new char[other.size() + 1];
      // Fill the allocated memory with the second string data
      for(int i=0; i<other.size(); i++) {
       data[i] = other.data[i];
      }
      // Add the null character at the end
      data[other.size()] = '\0';
      // Return itself
      return *this;
    }
}

// Concatenation with string
String operator+(const String &other) {
    int size1 = size();
    int size2 = other.size();
    // Allocate memory for the char array
    char* sum = new char[size1 + size2 + 1];

    // Fill the char array with the first string
    for(int i=0; i<size1; i++) {
      sum[i] = data[i];
    }
    // Append the char array with the second string
    for(int i=0; i<size2; i++) {
      sum[size1 + i] = other.data[i];
    }
    // Add the null character
    sum[size1 + size2] = '\0';

    // Create resulting string
    String result = String(sum);
    // Free allocated memory
    delete [] sum;
    // Return the resulting string object
    return result;
}

// String subtraction
String operator-(const String &other) {
    // If the size of the second string is greater,
    // return just the first string
    if (other.size() > size()) {
      return String(*this);
    }
    // Else, check if we can find the second string
    // within the first one
    else {
      int index = 0;
      int size1 = size();
      int size2 = other.size();
      bool found = false;
      // Compare each substring of size2 starting at index cnt
      while(index <= size1 - size2) {
       int cnt = 0;
       while(data[index+cnt] == other.data[cnt]) {
          cnt++;
          if(cnt == size2) {
            // If we found the match, set found to true and break
            found = true;
            break;
          }
       }
       // If the match is found, break (index will be the index where
       // the matching substring starts in the first string)
       if(found) {
          break;
       }
       // Else, increment the index to search for the next substring
       else {
          index++;
       }
      }

      // If found, create a new string
      // Otherwise, just return the copy of the first string
      if(found) {
       // Allocate memory for the char array
       char* diff = new char[size1 - size2 + 1];
       // Fill the char array skipping the letters of the second string
       for(int i=0; i<size1; i++) {
          if(i < index) {
            diff[i] = data[i];
          }
          else if(i >= index+size2) {
            diff[i-size2] = data[i];
          }
       }
       // Add null at the end
       diff[size1 - size2] = '\0';
       // Create the resulting string object with diff
       String result = String(diff);
       // Free the allocated memory
       delete [] diff;
       // Return the newly created string
       return result;
      }
      else {
       return String(*this);
      }
    }
}...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$50.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats