all MyStrings must always be stored in a dynamic array that is exactly the corre
ID: 3602757 • Letter: A
Question
all MyStrings must always be stored in a dynamic array that is exactly the correct size to store the string.
Extraction Operator
Just like the >> operator that reads C-strings, your >> operator should skip any leading spaces and then read characters into the string up to the first whitespace character.
For reasons of convenience, we will impose a limit of 127 on the number of characters this function will read. This is so you can temporarily read into a non-dynamic array and then copy what you need into your data member, which will be a dynamic array. Note that this does not mean that all MyStrings will always have a maximum of 127 characters. For example, you might get a MyString with more than 127 characters by using the MyString constructor or by concatenating two MyStrings.
Hint: Don't try to read character by character in a loop. Use the extraction operator to do the reading of the input into a non-dynamic array, then use strcpy() to copy it into your data member. Make sure to allocate the correct amount of memory.
Hint: if you use the extraction operator as suggested above, you will not have to skip leading whitespace, because the extraction operator does that for you.
A read() function
The read() function will allow the client programmer to specify the delimiting character (the one to stop at instead of the first space). This will be a void function that will take two arguments, a stream and the delimiting character. It should not skip leading spaces. The limit of 127 characters imposed on the >> function above also applies to this function.
Hint: Don't try to read character by character in a loop. Use the in.getline() function to do the reading of the input into a non-dynamic array, then use strcpy() to copy it into your data member.
Concatenation Operator
Overload the + operator to do MyString concatenation. The operator must be able to handle either MyString objects or C-strings on either side of the operator. Be careful with the memory management here. You'll have to allocate enough memory to hold the new MyString. I suggest using strcpy() to get the left operand into the result MyString, and then strcat() to append the right operand. Both strcpy() and strcat() should be used as if they are void, even though they do have return values.
Combined Concatenation/Assignment Operator
Overload the shorthand += to combine concatenation and assignment. Only MyStrings can be on the left-hand side of a += operation, but either MyStrings or C-strings may appear on the right side. If you pay close attention to the += operator from the feetInches class, these may be the easiest points of the semester.
Add Documentation
Hints about reading input files:
I suggest that you copy the text from the input file webpage and paste it into a file that you have created using your IDE. The files you create when you type in your IDE are always text files (even if they don't end with .txt). If you're using Windows, you could also use Notepad, but there's no reason to open another application when you are already working in your IDE. I strongly suggest that you don't use TextEdit (Mac) or Word, because these do not store files as text files by default.
Here is important information about where to save your input file so that your IDE can find it
Extensions are part of the file name. If you want read from a file named "myfile.txt", typing "myfile" or "myfile.cpp" won't work.
For Windows users, before you start this assignment, I suggest that you make sure that Windows is showing you the complete file name of your files, including the extensions. Windows hides this from you by default. In Windows 7 and 8 the procedure is as follows:
Choose "control panel" from the start menu
Choose "Folder Options" from the list of control panel items. In Windows 8 you may have to type "Folder Options" into the control panel search bar.
Choose the "view" tab from the Folder Options window
Find the checkbox that says "Hide extensions for known file types".
Uncheck that checkbox.
In Windows 10 the procedure is: go to file explorer and click "View" on the toolbar, then go to "Options" on the furthest right, click on the drop down arrow and choose "Change folder and search options". Click "View" on the top and uncheck "Hide extensions for known file types"
client
data file
Explanation / Answer
myString.h
#ifndef myString_h
#define myString_h
#include <iostream>
namespace cs_mystring {
class myString
{
private:
char *desc;
public:
/*
Preconditon: Takes no parameter and return nothing.
Postcondition: Initialize data member-'desc' with memory of size 1 and
copy empty string to it
*/
myString();
/*
Preconditon: Takes one argument - pointer to char object 'inDesc' and
is kept const
Postcondition: object is created with size accordingly with specified
object pointer given at input
*/
myString(const char *inDesc);
/*
Preconditon: Takes one argument - reference to myString object 'right'
and is kept const
Postcondition: object is created with size accordingly with specified
object reference given at input
*/
myString(const myString &right);
/* Preconditon: an myString object exist with valid length
Postcondition: myString object is destroyed
*/
~myString();
char operator[](int index)const;
char& operator[](int index);
/*
Preconditon: length donot take any argument
Postcondition: returns length of myString object
*/
long length()const;
/* Precondition: The out of ostream object is waiting to receive the
myString output.
Postcondition: myString source has been passed by reference to the
ostream out and is kept const.
*/
friend std::ostream& operator<<(std::ostream& out, const myString& source);
/*
Preconditon: Input argument contains address to myString object:-Right.
Postcondition: object is copied with size accordingly with specified
object reference given at input
*/
myString operator=(const myString& right);
/*
Preconditon: Input argument contains address to myString object:-Right.
Postcondition: returns pointer to this object which was created by
combination of concatenation and assignment operator
*/
myString operator+=(const myString& right);
//myString operator=(const myString &right);
friend bool operator>(const myString &left, const myString &right);
friend bool operator<(const myString &left, const myString &right);
friend bool operator>=(const myString &left, const myString &right);
friend bool operator<=(const myString &left, const myString &right);
friend bool operator==(const myString &left, const myString &right);
friend bool operator!=(const myString &left, const myString &right);
friend std::istream& operator>>(std::istream& in, myString& source);
friend myString operator+(const myString& left, const myString& right);
void read(std::istream& inString, char delim);
};
}
#endif
myString.cpp
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <cassert>
#include "myString.h"
using namespace std;
namespace cs_mystring {
myString::myString()
{
desc = new char[1];
strcpy(desc, "");
}
myString::myString(const char *inDesc)
{
desc = new char[strlen(inDesc) + 1];
strcpy(desc, inDesc);
}
myString::myString(const myString& right)
{
desc = new char[strlen(right.desc) + 1];
strcpy(desc, right.desc);
}
myString::~myString()
{
delete [] desc;
}
myString myString::operator=(const myString &right)
{
if (this != &right)
{
delete [] desc;
desc = new char[strlen(right.desc) + 1];
strcpy(desc, right.desc);
}
return *this;
}
char myString::operator[](int index)const
{
assert(index >= 0 && index < strlen(desc));
return desc[index];
}
char& myString::operator[](int index)
{
assert(index >= 0 && index < strlen(desc));
return desc[index];
}
// will return the length of the string without changing the calling object
long myString::length()const
{
long length = strlen(desc);
return length;
}
// insertion overload
ostream& operator<<(ostream& out, const myString& source)
{
out << source.desc;
return out;
}
// RELATIONAL OPERATORS
bool operator>(const myString &left, const myString &right)
{
if(strcmp(left.desc, right.desc) > 0)
return true;
return false;
}
bool operator<(const myString &left, const myString &right)
{
if(strcmp(left.desc, right.desc) < 0)
return true;
return false;
}
bool operator>=(const myString &left, const myString &right)
{
if(strcmp(left.desc, right.desc) >= 0)
return true;
return false;
}
bool operator<=(const myString &left, const myString &right)
{
if(strcmp(left.desc, right.desc) <= 0)
return true;
return false;
}
bool operator==(const myString &left, const myString &right)
{
if(strcmp(left.desc, right.desc) == 0)
return true;
return false;
}
bool operator!=(const myString &left, const myString &right)
{
if(strcmp(left.desc, right.desc) != 0)
return true;
return false;
}
// temp will be a temporary char array that we will use to hold the values
// of the instream
istream& operator>>(istream& in, myString& source)
{
while (isspace(in.peek())){
in.ignore();
}
char temp[128];
in.getline(temp, 127, ' ');
delete [] source.desc;
source.desc = new char[strlen(temp) +1];
strcpy(source.desc, temp);
return in;
}
// the strLength variable will hold the length of the two strings and
// will add one for the delimiter character
//
// temp will be a temporary string that will be returned as a
result of the
// addition, since both of the arguments are consts
myString operator+(const myString& left, const myString&
right)
{
long strLength = strlen(left.desc);
strLength += strlen(right.desc);
myString temp = new char[strLength + 1];
strcpy(temp.desc, left.desc);
strcat(temp.desc, right.desc);
return temp;
}
// char is a temp array that we will use to read in the
instream file,
// and we will rely on strcpy on how to add it into our this
variable
void myString::read(std::istream& inString, char delim)
{
char temp[128];
inString.getline(temp, 127, ' ');
delete [] this->desc;
this->desc = new char[strlen(temp) +1];
strcpy(this->desc, temp);
}
// the strLength variable is used to hold the length of both
of the variables
// and add one for the ending character
//
// the temp myString object is used to temporarily hold the
value of the char
// array before we copy it in
myString myString::operator+=(const myString& right)
{
long strLength = strlen(this->desc) + strlen(right.desc) +
1;
myString temp = new char[strLength];
strcpy(temp.desc, this->desc);
delete [] this->desc;
this->desc = new char[strLength];
strcpy(this->desc, temp.desc);
strcat(this->desc, right.desc);
return *this;
}
}
main.cpp
#include "myString.h"
#include <fstream>
#include <cctype> // for toupper()
#include <string>
#include <cassert>
#include <iostream>
using namespace std;
using namespace cs_mystring;
bool eof(istream& in);
void BasicTest();
void RelationTest();
void ConcatTest();
void CopyTest();
myString AppendTest(const myString& ref, myString val);
string boolString(bool convertMe);
int main()
{
BasicTest();
RelationTest();
ConcatTest();
CopyTest();
system("pause");
return 0;
}
bool eof(istream& in)
{
char ch;
in >> ch;
in.putback(ch);
return !in;
}
string boolString(bool convertMe) {
if (convertMe) {
return "true";
} else {
return "false";
}
}
void BasicTest()
{
myString s;
cout << "----- Testing basic String creation & printing" << endl;
const myString strs[] =
{myString("Wow"), myString("C++ is neat!"),
myString(""), myString("a-z")};
for (int i = 0; i < 4; i++){
cout << "string [" << i <<"] = " << strs[i] << endl;
}
cout << endl << "----- Now reading MyStrings from file" << endl;
cout << endl << "----- first, word by word" << endl;
ifstream in("mystring.txt");
assert(in);
while (in.peek() == '#'){
in.ignore(128, ' ');
}
in >> s;
while (in) {
cout << "Read string = " << s << endl;
in >> s;
}
in.close();
cout << endl << "----- now, line by line" << endl;
ifstream in2("mystring.txt");
assert(in2);
while (in2.peek() == '#'){
in2.ignore(128, ' ');
}
s.read(in2, ' ');
while (in2) {
cout << "Read string = " << s << endl;
s.read(in2, ' ');
}
cout << endl << "----- Testing access to characters (using const)" << endl;
const myString s1("abcdefghijklmnopqsrtuvwxyz");
cout << "Whole string is " << s1 << endl;
cout << "now char by char: ";
for (int i = 0; i < s1.length(); i++){
cout << s1[i];
}
cout << endl << "----- Testing access to characters (using non-const)" << endl;
myString s2("abcdefghijklmnopqsrtuvwxyz");
cout << "Start with " << s2;
for (int i = 0; i < s2.length(); i++){
s2[i] = toupper(s2[i]);
}
cout << " and convert to " << s2 << endl;
}
void RelationTest()
{
cout << " ----- Testing relational operators between MyStrings ";
const myString strs[] =
{myString("app"), myString("apple"), myString(""),
myString("Banana"), myString("Banana")};
for (int i = 0; i < 4; i++) {
cout << "Comparing " << strs[i] << " to " << strs[i+1] << endl;
cout << " Is left < right? " << boolString(strs[i] < strs[i+1]) << endl;
cout << " Is left <= right? " << boolString(strs[i] <= strs[i+1]) << endl;
cout << " Is left > right? " << boolString(strs[i] > strs[i+1]) << endl;
cout << " Is left >= right? " << boolString(strs[i] >= strs[i+1]) << endl;
cout << " Does left == right? " << boolString(strs[i] == strs[i+1]) << endl;
cout << " Does left != right ? " << boolString(strs[i] != strs[i+1]) << endl;
}
cout << " ----- Testing relations between MyStrings and char * ";
myString s("he");
const char *t = "hello";
cout << "Comparing " << s << " to " << t << endl;
cout << " Is left < right? " << boolString(s < t) << endl;
cout << " Is left <= right? " << boolString(s <= t) << endl;
cout << " Is left > right? " << boolString(s > t) << endl;
cout << " Is left >= right? " << boolString(s >= t) << endl;
cout << " Does left == right? " << boolString(s == t) << endl;
cout << " Does left != right ? " << boolString(s != t) << endl;
myString u("wackity");
const char *v = "why";
cout << "Comparing " << v << " to " << u << endl;
cout << " Is left < right? " << boolString(v < u) << endl;
cout << " Is left <= right? " << boolString(v <= u) << endl;
cout << " Is left > right? " << boolString(v > u) << endl;
cout << " Is left >= right? " << boolString(v >= u) << endl;
cout << " Does left == right? " << boolString(v == u) << endl;
cout << " Does left != right ? " << boolString(v != u) << endl;
}
void ConcatTest()
{
cout << " ----- Testing concatentation on MyStrings ";
const myString s[] =
{myString("outrageous"), myString("milk"), myString(""),
myString("cow"), myString("bell")};
for (int i = 0; i < 4; i++) {
cout << s[i] << " + " << s[i+1] << " = " << s[i] + s[i+1] << endl;
}
cout << " ----- Testing concatentation between myString and char * ";
const myString a("abcde");
const char *b = "XYZ";
cout << a << " + " << b << " = " << a + b << endl;
cout << b << " + " << a << " = " << b + a << endl;
cout << " ----- Testing shorthand concat/assign on MyStrings ";
myString s2[] =
{myString("who"), myString("what"), myString("WHEN"),
myString("Where"), myString("why")};
for (int i = 0; i < 4; i++) {
cout << s2[i] << " += " << s2[i+1] << " = ";
cout << (s2[i] += s2[i+1]) << "and";
cout << s2[i] << endl;
}
cout << " ----- Testing shorthand concat/assign using char * ";
myString u("I love ");
const char *v = "programming";
cout << u << " += " << v << " = ";
cout << (u += v) << endl;
}
myString AppendTest(const myString& ref, myString val)
{
val[0] = 'B';
return val + ref;
}
void CopyTest()
{
cout << " ----- Testing copy constructor and operator= on MyStrings ";
myString orig("cake");
myString copy(orig); // invoke copy constructor
copy[0] = 'f'; // change first letter of the *copy*
cout << "original is " << orig << ", copy is " << copy << endl;
myString copy2; // makes an empty string
copy2 = orig; // invoke operator=
copy2[0] = 'f'; // change first letter of the *copy*
cout << "original is " << orig << ", copy is " << copy2 << endl;
copy2 = "Copy Cat";
copy2 = copy2; // copy onto self and see what happens
cout << "after self assignment, copy is " << copy2 << endl;
cout << "Testing pass & return MyStrings by value and ref" << endl;
myString val = "winky";
myString sum = AppendTest("Boo", val);
cout << "after calling Append, sum is " << sum << endl;
cout << "val is " << val << endl;
val = sum;
cout << "after assign, val is " << val << endl;
}
Result:
----- Testing basic String creation & printing
string [0] = Wow
string [1] = C++ is neat!
string [2] =
string [3] = a-z
----- Now reading MyStrings from file
----- first, word by word
Read string = The
Read string = first
Read string = time
Read string = we
Read string = will
Read string = read
Read string = individual
Read string = words,
Read string = next
Read string = we
Read string = read
Read string = whole
Read string = lines
----- now, line by line
Read string = The first time we will
Read string = read individual words, next
Read string = we read whole lines
----- Testing access to characters (using const)
Whole string is abcdefghijklmnopqsrtuvwxyz
now char by char: abcdefghijklmnopqsrtuvwxyz
----- Testing access to characters (using non-const)
Start with abcdefghijklmnopqsrtuvwxyz and convert to ABCDEFGHIJKLMNOPQSRTUVWXYZ
----- Testing relational operators between MyStrings
Comparing app to apple
Is left < right? true
Is left <= right? true
Is left > right? false
Is left >= right? false
Does left == right? false
Does left != right ? true
Comparing apple to
Is left < right? false
Is left <= right? false
Is left > right? true
Is left >= right? true
Does left == right? false
Does left != right ? true
Comparing to Banana
Is left < right? true
Is left <= right? true
Is left > right? false
Is left >= right? false
Does left == right? false
Does left != right ? true
Comparing Banana to Banana
Is left < right? false
Is left <= right? true
Is left > right? false
Is left >= right? true
Does left == right? true
Does left != right ? false
----- Testing relations between MyStrings and char *
Comparing he to hello
Is left < right? true
Is left <= right? true
Is left > right? false
Is left >= right? false
Does left == right? false
Does left != right ? true
Comparing why to wackity
Is left < right? false
Is left <= right? false
Is left > right? true
Is left >= right? true
Does left == right? false
Does left != right ? true
----- Testing concatentation on MyStrings
outrageous + milk = outrageousmilk
milk + = milk
+ cow = cow
cow + bell = cowbell
----- Testing concatentation between myString and char *
abcde + XYZ = abcdeXYZ
XYZ + abcde = XYZabcde
----- Testing shorthand concat/assign on MyStrings
who += what = whowhatandwhowhat
what += WHEN = whatWHENandwhatWHEN
WHEN += Where = WHENWhereandWHENWhere
Where += why = WherewhyandWherewhy
----- Testing shorthand concat/assign using char *
I love += programming = I love programming
----- Testing copy constructor and operator= on MyStrings
original is cake, copy is fake
original is cake, copy is fake
after self assignment, copy is Copy Cat
Testing pass & return MyStrings by value and ref
after calling Append, sum is BinkyBoo
val is winky
after assign, val is BinkyBoo
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.