#include <cstring> // for
strlen()
#include <cassert> // for
assert()
#include <iostream> // for
cout
using namespace std;
class MyString
{
private:
char *m_data;
int m_length;
public:
MyString(const char *source =
"")
{
assert(source);
// make sure source isn't a null string
// Find the
length of the string
// Plus one
character for a terminator
m_length =
strlen(source) + 1;
// Allocate a
buffer equal to this length
m_data = new
char[m_length];
// Copy the
parameter string into our internal buffer
for (int i = 0;
i < m_length; ++i)
m_data[i] = source[i];
// Make sure the
string is terminated
m_data[m_length-1] = '\0';
}
// 1. Your copy constructor code goes here
// 2. Your assignment operator code goes here
// 3. Your overloaded operator<< code goes here
~MyString() // destructor
{
// We need to deallocate our string
delete[] m_data;
}
char* getString() { return m_data; }
int getLength() { return m_length; }
};
int main()
{
char* testOutput = "Test String Here";
MyString original(testOutput);
cout << "\n original object holds string : "
<< original << endl;
char* newOutput = "copied successfully using copy
constructor!";
MyString notOriginal(newOutput);
cout << "\n notOriginal object holds string: "
<< notOriginal << endl;
cout << "\n Now, let's copy notOriginal object
to displayCopiedString object " << endl;
// notOriginal is an object, not a char*
MyString displayCopiedString (notOriginal);
cout << "\n displayCopiedString: " <<
displayCopiedString << "as well" << endl;
}
//Modified C program
#include <cstring> // for strlen()
#include <cassert> // for assert()
#include <iostream> // for cout
using namespace std;
class MyString
{
private:
char *m_data;
int m_length;
public:
MyString(const char *source = "")
{
assert(source); // make sure source isn't a null string
// Find the length of the string
// Plus one character for a terminator
m_length = strlen(source) + 1;
// Allocate a buffer equal to this length
m_data = new char[m_length];
// Copy the parameter string into our internal buffer
for (int i = 0; i < m_length; ++i)
m_data[i] = source[i];
// Make sure the string is terminated
m_data[m_length-1] = '\0';
}
// 1. Your copy constructor code goes here
MyString(const MyString & str){
m_length = str.m_length;
// Allocate a buffer equal to this length
m_data = new char[m_length];
// Copy the parameter string into our internal buffer
for (int i = 0; i < m_length; ++i)
m_data[i] = str.m_data[i];
// Make sure the string is terminated
m_data[m_length-1] = '\0';
}
// 2. Your assignment operator code goes here
MyString & operator =(const MyString & str){
MyString ob;
ob.m_length = str.m_length;
// Allocate a buffer equal to this length
ob.m_data = new char[m_length];
// Copy the parameter string into our internal buffer
for (int i = 0; i < ob.m_length; ++i)
ob.m_data[i] = str.m_data[i];
// Make sure the string is terminated
ob.m_data[ob.m_length-1] = '\0';
return ob;
}
// 3. Your overloaded operator<< code goes here
friend ostream& operator <<(ostream&os,const
MyString&str){
for (int i = 0; i < str.m_length; ++i)
os<<str.m_data[i];
return os;
}
~MyString() // destructor
{
// We need to deallocate our string
delete[] m_data;
}
char* getString() { return m_data; }
int getLength() { return m_length; }
};
int main()
{
char* testOutput = "Test String Here";
MyString original(testOutput);
cout << "\n original object holds string : " <<original
<< endl;
char* newOutput = "copied successfully using copy
constructor!";
MyString notOriginal(newOutput);
cout << "\n notOriginal object holds string: " <<
notOriginal << endl;
cout << "\n Now, let's copy notOriginal object to
displayCopiedString object " << endl;
// notOriginal is an object, not a char*
MyString displayCopiedString (notOriginal);
cout << "\n displayCopiedString: " <<
displayCopiedString << "as well" << endl;
}
//sample output

#include <cstring> // for strlen() #include <cassert> // for assert() #include <iostream> //...
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...
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 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&...
Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will call our version MyString. This object is designed to make working with sequences of characters a little more convenient and less error-prone than handling raw c-strings, (although it will be implemented as a c-string behind the scenes). The MyString class will handle constructing strings, reading/printing, and accessing characters. In addition, the MyString object will have the ability to make a full deep-copy of itself...
Use C++
#include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstring> using namespace std; /I Copy n characters from the source to the destination. 3 void mystrncpy( ???) 25 26 27 28 29 11- 30 Find the first occurrance of char acter c within a string. 32 ??? mystrchr???) 34 35 36 37 38 39 / Find the last occurrance of character c within a string. 40 II 41 ??? mystrrchr ???) 42 43 45 int main() char userInput[ 81]; char...
#include "stdafx.h" #include <iostream> #include <vector> #include <cassert> using namespace std; // function prototypes int loadDisk(vector<int> &firstPeg, int numDisks); void printPeg(vector<int> &peg); int hanoi(struct pegType &startPeg, struct pegType &swapPeg, struct pegType &endPeg, int numDisk); void moveDisk(struct pegType &startPeg, struct pegType &endPeg); struct pegType { vector <int> diskStack; int name; }; int main() { int numDisk = 7; int i; pegType peg1, peg2, peg3; peg1.name = 1; peg2.name = 2; peg3.name = 3;...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector { public: Vector( int initsize = 0 ) : theSize( initsize ), theCapacity( initsize + SPARE_CAPACITY ) { objects = new T[ theCapacity ]; } Vector( const Vector & rhs ) : theSize( rhs.theSize), theCapacity( rhs.theCapacity ), objects( 0 ) { objects = new T[ theCapacity ]; for( int k = 0; k < theSize; ++k) objects[ k ] = rhs.objects[ k...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0; i<expr.length(); i++){ //Insert code here to push each character onto the stack } cout << "My stack is popped in this order" << endl; while(!myStack.empty()){ //Insert code here to cout the top of the stack one by one //Pop each one after it’s printed out } cout << endl; } void printFromQueue(string expr){ queue<char> myQueue; //Insert code here to push each character onto the...