Question

SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE

To gain experience with the dynamic data structures (allocation, automatic expansion, deletion) and the big three concepts:

PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!!

mystring.h:

//File: mystring1.h

// ================

// Interface file for user-defined String class.

#ifndef _MYSTRING_H

#define _MYSTRING_H

#include<iostream>

#include <cstring> // for strlen(), etc.

using namespace std;

#define MAX_STR_LENGTH 200

class String {

public:

String();

String(const char s[]); // a conversion constructor

void append(const String &str);

// Relational operators

bool operator ==(const String &str) const;

bool operator !=(const String &str) const;

bool operator >(const String &str) const;

bool operator <(const String &str) const;

bool operator >=(const String &str) const;

String operator +=(const String &str);

void print(ostream &out) const;

int length() const;

char operator [](int i) const; // subscript operator

private:

char contents[MAX_STR_LENGTH+1];

int len;

};

ostream & operator<<(ostream &out, const String & r); // overload ostream

operator "<<" - External!

#endif /* not defined _MYSTRING_H */

mystring.cpp:

//File: mystring1.h

// ================

// Interface file for user-defined String class.

#include"mystring1.h”

String::String()

{

contents[0] = '\0';

len = 0;

}

String::String(const char s[])

{

len = strlen(s);

strcpy(contents, s);

}

void String::append(const String &str)

{

strcat(contents, str.contents);

len += str.len;

}

bool String::operator ==(const String &str) const

{

return strcmp(contents, str.contents) == 0;

}

bool String::operator !=(const String &str) const

{

return strcmp(contents, str.contents) != 0;

}

bool String::operator >(const String &str) const

{

return strcmp(contents, str.contents) > 0;

}

bool String::operator <(const String &str) const

{

return strcmp(contents, str.contents) < 0;

}

bool String::operator >=(const String &str) const

{

return strcmp(contents, str.contents) >= 0;

}

String String::operator +=(const String &str)

{

append(str);

return *this;

}

void String::print(ostream &out) const

{

out << contents;

}

int String::length() const

{

return len;

}

char String::operator [](int i) const

{

if (i < 0 || i >= len) {

cerr << "can't access location " << i

<< " of string \"" << contents << "\"" << endl;

return '\0';

}

return contents[i];

}

ostream & operator<<(ostream &out, const String & s) // overload ostream

operator "<<" - External!

{

s.print(out);

return out;

}

mystringDriver.cpp:

#include <iostream>

#include "mystring2.h”

using namespace std;

/************************ Function Prototypes ************************/

/*

* Function: PrintString

* Usage: PrintString(str);

*

* Prints out the value and length of the String object passed to it.

*/

void PrintString(const char *label,

const String &str); // overloaded ostream operator <<

is used in the definition.

/*************************** Main Program **************************/

int main()

{

String str1, str2("init2"), str3 = "init3"; // Some String objects.

Using constructor for copy

char s1[100], s2[100], s3[100]; // Some character strings.

// Print out their initial values...

cout << "Initial values:" << endl;

PrintString("str1", str1);

PrintString("str2", str2);

PrintString("str3", str3);

// Store some values in them...

cout << "\nEnter a value for str1 (no spaces): ";

cin >> s1;

str1 = s1;

cout << "\nEnter a value for str2 (no spaces): ";

cin >> s2;

str2 = s2;

cout << "\nEnter a value for str3 (no spaces): ";

cin >> s3;

str3 = s3;

cout << "\nAfter assignments..." << endl;

PrintString("str1", str1);

PrintString("str2", str2);

PrintString("str3", str3);

// Access some elements...

int i;

cout << "\nEnter which element of str1 to display: ";

cin >> i;

cout << "Element #" << i << " of str1 is '" << str1[i]

<< "'" << endl;

cout << "\nEnter which element of str2 to display: ";

cin >> i;

cout << "Element #" << i << " of str2 is '" << str2[i]

<< "'" << endl;

cout << "\nEnter which element of str3 to display: ";

cin >> i;

cout << "Element #" << i << " of str3 is '" << str3[i]

<< "'" << endl;

// Append some strings...

cout << "\nEnter a value to append to str1 (no spaces): ";

cin >> s1;

// str1.append(s1); // Actually, the cstring is converted to String

object here by the constructor

str1 += s1; // same as above

cout << "\nEnter a value to append to str2 (no spaces): ";

cin >> s2;

str2 += s2;

cout << "\nEnter a value to append to str3 (no spaces): ";

cin >> s3;

str3 += s3;

cout << "\nAfter appending..." << endl;

PrintString("str1", str1);

PrintString("str2", str2);

PrintString("str3", str3);

// Compare some strings...

cout << "\nComparing str1 and str2..." << endl;

cout << "\"";

cout<< str1; // test the overloading of ostream operator <<

cout << "\" is ";

if (str1 < str2) { // test the overloading of comparison operator <

cout << "less than";

} else if (str1 > str2) {

cout << "greater than";

} else {

cout << "equal to";

}

cout << " \"";

cout << str2;

cout << "\"" << endl;

cout << "\ntest the = operator, after str1 = str2; "<< endl;

str1 = str2;

PrintString("str1", str1);

PrintString("str2", str2);

str1 += s1;

cout << "\nAfter str1 = str1 + s1: "<< endl;

PrintString("str1", str1);

PrintString("str2", str2);

String str4(str3);

cout << "\ntest the copy constructor, after str4(str3);"<< endl;

PrintString("str3", str3);

PrintString("str4", str4);

cout << "\nafter appending str3 by str2" << endl;

str3 += str2;

PrintString("str3", str3);

PrintString("str4", str4);

cout<< "\nstr2, str4 are not changed. Type any letter to quit." << endl;

char q;

cin >> q;

return 0;

}

/*********************** Function Definitions **********************/

void PrintString(const char *label,

const String &str)

{

cout << label << " holds \"";

cout << str; // << is overloaded

cout << "\" (length = " << str.length() << ")" << endl;

}

PLEASE ONLY LEAVE A SCREENSHOT OF THE CODE! NO TEXT FILES! OUTPUT AND COMMENTS WOULD BE APPRECIATED AS WELL!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

mystring1.h header file

Quick Launch (Ctrl+Q) String Class-Microsoft Visual Studio FILE EDIT VIEW PROJECT BUILD DEBUG TEAM SQL TOOLS TEST ARCHITECTUR

mystring.cpp file

String Class mystring-cpp X String length0 const #include mystring! h //default constructor String: :String() contents側ㄧ ·String Class mystring-cpp X String length() const return strcmp(contents, str.contents) θ; < bool String::operator -(const St

mysstringDriver.cpp

String Class Global Scope main #include <iostream #include mystring!,h using namespace std; Function: PrintString Usage: PriString Class Global Scope main PrintStr ing( str3, str3); Access some elements. int i cout<LnEnter which element of strl tString Class Global Scope main else if (stri>str2) cout <<greater than cout<equal to cout << cout<str2; cout<<<endl; couString Class Global Scope main PrintString(str2, str2) strls1; cout<nAfter str1-str1+s1: <<endl; PrintString(str1, str1

output

Initial values: str1 holds(length e) str2 holds init2 (length 5) str3 holds init3 (length 5) Enter a value for str1 (no sEnter a value to append to str1 (no spaces): Programming Enter a value to append to str2 (no spaces): &Technology Enter a val

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are to implement a MyString class which is our own limited implementation of the std::string...

    You are to implement a MyString class which is our own limited implementation of the std::string Header file and test (main) file are given in below, code for mystring.cpp. Here is header file mystring.h /* MyString class */ #ifndef MyString_H #define MyString_H #include <iostream> using namespace std; class MyString { private:    char* str;    int len; public:    MyString();    MyString(const char* s);    MyString(MyString& s);    ~MyString();    friend ostream& operator <<(ostream& os, MyString& s); // Prints string    MyString& operator=(MyString& s); //Copy assignment    MyString& operator+(MyString&...

  • Using C++ Use the below program. Fill in the code for the 2 functions. The expected...

    Using C++ Use the below program. Fill in the code for the 2 functions. The expected output should be: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: #include #include #include using namespace std; //Assume a line is less than 256 //Prints the characters in the string str1 from beginIndex //and inclusing endIndex void printSubString( const char* str1 , int beginIndex, int endIndex ) { } void printWordsInAString( const char* str1 ) { } int main() { char str1[] = "The fox jumps over the...

  • Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with...

    Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with a new string. * Append an s to the end of your input string if it's not a keyword. * Insert a period at the end, if you have an empty input string do not insert a period Problems: //Why does my code stop at only 2 input strings //If I insert a character my empty string case works but it's still bugged where...

  • Write a MyString class that stores a (null-terminated) char* and a length and implements all of...

    Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator: prints "Move assignment" and endl in addition...

  • Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2...

    Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2 - Stack around the variable 'string4b' was corrupted. I need some help because if the arrays for string4a and string4b have different sizes. Also if I put different sizes in string3a and string4b it will say that those arrays for 3a and 3b are corrupted. But for the assigment I need to put arrays of different sizes so that they can do their work...

  • C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all...

    C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Submit your completed source (.cpp) file. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator:...

  • You are to write two functions, printString() and testString(), which are called from the main function....

    You are to write two functions, printString() and testString(), which are called from the main function. printString (string) prints characters to std::cout with a space after every character and a newline at the end. testString (string) returns true if the string contains two consecutive characters that are the same, false otherwise. See the main() to see how the two functions are called. Some sample runs are given below: string: “hello” printString prints: h e l l o testString returns: true...

  • In C++: You are to write two functions, printString() and testString(), which are called from the...

    In C++: You are to write two functions, printString() and testString(), which are called from the main function. printString (string) prints every character in the string to std::cout with a space after every character and a newline at the end. testString (string) returns true if the string contains characters that are in sorted order, false otherwise. You may assume that all characters are lowercase and only alphabetical characters are present. See the main() to see how the two functions are...

  • The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is...

    The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is wrong with the code written. I get errors about stockSym being private and some others after that.This was written in the Dev C++ software. Can someone help me figure out what is wrong with the code with notes of what was wrong to correct it? #include <cstdlib> #include <iostream> #include <iomanip> #include <fstream> #include <cassert> #include <string> using namespace std; template <class stockType> class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT