Question

In: Computer Science

C++ For this assignment, you will implement the MyString class. Like the string class in C++’s...

C++

For this assignment, you will implement the MyString class. Like the string class in C++’s standard library, this class uses C-strings as the underlying data structure. Recall that a C-string is a null-terminated array of type char. See section 8.1 in Savitch for more information about C-strings; in particular, you will find it helpful to take advantage of several of the C-string functions mentioned in that section. What To Do. In the hw8 directory that is created, you will find the following files: • mystring.h and mystring.cpp • a Makefile • and 4 test files (test1.cpp through test4.cpp). (Below are the files mentioned here, with one sample file). Implement the following: 1. a default constructor 2. a constructor that takes a const char * parameter (that is, a C-string) 3. a destructor 4. a copy constructor 5. the assignment operator. In addition, overload the following relational operators: >, <, >=, <=, ==, and !=. Lastly, overload the operator+ (concatenation). All of the relational operators return type int. To understand why, read the description of the C-string function strcmp. All of the comparisons should be lexicographical, i.e., similar to what strcmp() does. The == and != operators should evaluate to 1 if the condition is true, 0 if false. You are welcome to implement some of the operators by using others (for example, you can easily implement != using ==). Make sure that you can invoke the operators with string literals on either side of the operator. That is, both of the following expressions should be valid: str == "hello" "hello" == str where str is a MyString object. Place the member function implementations in mystring.cpp and compile using make. You can also edit mystring.h if you wish to implement some of the member functions directly in the class definition

Make File:

CC = g++
CXX = g++

INCLUDES =

CFLAGS = -Wall $(INCLUDES)
CXXFLAGS = -Wall $(INCLUDES)

LDFLAGS =
LDLIBS =

executables = test1 test2 test3 test4
objects = mystring.o test1.o test2.o test3.o test4.o

.PHONY: default
default: $(executables)

$(executables): mystring.o

$(objects): mystring.h


.PHONY: clean
clean:
   rm -f *~ a.out core $(objects) $(executables)

.PHONY: all
all: clean default

mystring.cpp:

#include

#include

#include "mystring.h"

// Insertion (put-to) operator

std::ostream& operator<<(std::ostream& outs, const MyString& s)

{

outs << s.data;

return outs;

}

// Extraction (get-from) operator

std::istream& operator>>(std::istream& is, MyString& s)

{

// Though this cheats a bit, it's meant to illustrate how this

// function can work.

  

std::string temp;

is >> temp;

delete[] s.data;

s.len = strlen(temp.c_str());

s.data = new char[s.len+1];

strcpy(s.data, temp.c_str());

return is;

}

mystring.h:

#ifndef _MYSTRING_H_

#define _MYSTRING_H_

#include

class MyString {

public:

// default constructor

MyString();

// constructor

MyString(const char* p);

// destructor

~MyString();

// copy constructor

MyString(const MyString& s);

// assignment operator

MyString& operator=(const MyString& s);

// returns the length of the string

int length() const { return len; }

// insertion (or put-to) operator

friend std::ostream& operator<<(std::ostream& outs, const MyString& s);

// extraction (or get-from) operator

friend std::istream& operator>>(std::istream& is, MyString& s);

// concatenates two strings

friend MyString operator+(const MyString& s1, const MyString& s2);

// relational operators

friend int operator<(const MyString& s1, const MyString& s2);

friend int operator>(const MyString& s1, const MyString& s2);

friend int operator==(const MyString& s1, const MyString& s2);

friend int operator!=(const MyString& s1, const MyString& s2);

friend int operator<=(const MyString& s1, const MyString& s2);

friend int operator>=(const MyString& s1, const MyString& s2);

private:

char* data;

int len;

};

#endif

Here is a sample test file:

#include "mystring.h"

static MyString add(MyString s1, MyString s2)

{

MyString temp(" and ");

return s1 + temp + s2;

}

int main()

{

using namespace std;

MyString s1("one");

MyString s2("two");

MyString s3 = add(s1, s2);

cout << s3 << endl;

return 0;

}

Solutions

Expert Solution

// mystring.h
#ifndef _MYSTRING_H_

#define _MYSTRING_H_

#include <iostream>

class MyString {

public:

// default constructor

MyString();

// constructor

MyString(const char* p);

// destructor

~MyString();

// copy constructor

MyString(const MyString& s);

// assignment operator

MyString& operator=(const MyString& s);

// returns the length of the string

int length() const { return len; }

// insertion (or put-to) operator

friend std::ostream& operator<<(std::ostream& outs, const MyString& s);

// extraction (or get-from) operator

friend std::istream& operator>>(std::istream& is, MyString& s);

// concatenates two strings

friend MyString operator+(const MyString& s1, const MyString& s2);

// relational operators

friend int operator<(const MyString& s1, const MyString& s2);

friend int operator>(const MyString& s1, const MyString& s2);

friend int operator==(const MyString& s1, const MyString& s2);

friend int operator!=(const MyString& s1, const MyString& s2);

friend int operator<=(const MyString& s1, const MyString& s2);

friend int operator>=(const MyString& s1, const MyString& s2);

private:

char* data;

int len;

};

#endif
//end of mystring.h

// mystring.cpp

#include <string>
#include <cstring>
#include "mystring.h"

MyString::MyString()
{
   data = '\0'; // initialize an empty string
   len = 0;
}

MyString::MyString(const char* p)
{
   len = strlen(p);
   data = new char[strlen(p)+1];
   strcpy(data,p);
}

MyString::~MyString()
{
   delete data;
}

MyString::MyString(const MyString& s)
{
   len = s.len;
   data = new char[len];
   strcpy(data,s.data);
}

MyString& MyString::operator=(const MyString& s)
{
   if(this != &s)
   {
       delete data;
       len = s.len;
       data = new char[len];
       strcpy(data,s.data);
   }

   return *this;
}

MyString operator+(const MyString& s1, const MyString& s2)
{
   char *p = new char[s1.length() + s2.length()+1];
   int len = s1.length() + s2.length();
   int i=0, j=0;
   for(;i<s1.length();i++)
       p[i] = s1.data[i];
   for(;i<len;i++,j++)
       p[i] = s2.data[j];
   p[i] = '\0';

   MyString sAdd(p);
   return sAdd;
}

int operator<(const MyString& s1, const MyString& s2)
{
   return (strcmp(s1.data,s2.data) < 0);
}

int operator>(const MyString& s1, const MyString& s2)
{
   return (strcmp(s1.data,s2.data) > 0);
}

int operator==(const MyString& s1, const MyString& s2)
{
   return (strcmp(s1.data,s2.data) == 0);
}

int operator!=(const MyString& s1, const MyString& s2)
{
   return(!(s1 == s2));
}

int operator<=(const MyString& s1, const MyString& s2)
{
   return (strcmp(s1.data,s2.data) <= 0);
}

int operator>=(const MyString& s1, const MyString& s2)
{
   return (strcmp(s1.data,s2.data) >= 0);
}

// Insertion (put-to) operator

std::ostream& operator<<(std::ostream& outs, const MyString& s)
{

outs << s.data;

return outs;

}

// Extraction (get-from) operator

std::istream& operator>>(std::istream& is, MyString& s)
{

// Though this cheats a bit, it's meant to illustrate how this

// function can work.

std::string temp;
is >> temp;

delete[] s.data;

s.len = strlen(temp.c_str());

s.data = new char[s.len+1];

strcpy(s.data, temp.c_str());

return is;

}


//end of mystring.cpp

// main.cpp : C++ program to test the MyString class
#include "mystring.h"

static MyString add(MyString s1, MyString s2)
{

   MyString temp(" and ");

   return s1 + temp + s2;

}

int main()
{

using namespace std;

   MyString s1("one");

   MyString s2("two");

   MyString s3 = add(s1, s2);

   cout << s3 << endl;

   return 0;

}
//end of main.cpp

Output:


Related Solutions

1. public class MyString { private char[] data; MyString(String string) { data = string.toCharArray(); } public...
1. public class MyString { private char[] data; MyString(String string) { data = string.toCharArray(); } public int compareTo(MyString other) { /* code from HW1 here */ } public char charAt(int i) { return data[i]; } public int length() { return data.length; } public int indexOf(char c) { /* code from HW1 here */ } public boolean equals(MyString other) { /* code from HW1 here */ } /* * Change this MyString by removing all occurrences of * the argument character...
In C++ Valid Palindrome In this assignment, you need to implement a bool isPalindrome(string s) function....
In C++ Valid Palindrome In this assignment, you need to implement a bool isPalindrome(string s) function. Given a string s, isPalindrome(s) can determine if s is a palindrome, considering only alphanumeric characters and ignoring cases. Note: for the purpose of this problem, we define empty string as valid palindrome. Example 1: Input: ”A man, a plan, a canal: Panama” Output: true Example 2: Input: ”race a car” Output: false Requirement: There are many methods to check if a string is...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
C++ ONLY! Implement the find function for the List class. It takes a string as an...
C++ ONLY! Implement the find function for the List class. It takes a string as an argument and returns an iterator to a matching node. If no matching node, it returns a null iterator. #include <iostream> #include <cstddef> #include <string> using Item = std::string; class List { private: class ListNode { public: Item item; ListNode * next; ListNode(Item i, ListNode *n=nullptr) { item = i; next = n; } };    ListNode * head; ListNode * tail;    public: class...
Problem Statement: Implement the MyString class using a header and implementation file named MyString.h and MyString.cpp...
Problem Statement: Implement the MyString class using a header and implementation file named MyString.h and MyString.cpp respectively. Make sure to properly test your code on your own by creating a test driver that tests every function created in the MyString class. Deliverables: proj3-MyString.h proj3-MyString.cpp proj3-testMain.cpp Memory Requirements: Your MyString should start with 10 bytes of allocated memory and should grow in size by doubling. So, we should be able to predict the capacity of your MyString as acquiring a patten...
C++ PROGRAMMING. S-DES: The purpose of this assignment is to implement algorithm for encryption with the...
C++ PROGRAMMING. S-DES: The purpose of this assignment is to implement algorithm for encryption with the simplified DES-type algorithm. · Item #1. Write a C++ program that performs one round of the simplified DES-type algorithm. Test your code with a plaintext = 011100100110 and K = 010011001.
Using STL stack class, implement in C++ the following pseudocodefunctioncheckBraces(aString: string) that checks for...
Using STL stack class, implement in C++ the following pseudocode functioncheckBraces(aString: string) that checks for balanced braces { } in a given string /arithmetic expressions. Write a main() program to test the function.// Checks the string aString to verify that braces match.// Returns true if aString contains matching braces, false otherwise.checkBraces(aString: string): boolean{aStack = a new empty stackbalancedSoFar = truei =0// Tracks character position in stringwhile (balancedSoFar and i < length of aString) {ch = character at position i in...
Here is a C++ class definition for an abstract data type LinkedList of string objects. Implement...
Here is a C++ class definition for an abstract data type LinkedList of string objects. Implement each member function in the class below. Some of the functions we may have already done in the lecture, that's fine, try to do those first without looking at your notes. You may add whatever private data members or private member functions you want to this class. #include #include typedef std::string ItemType; struct Node { ItemType value; Node *next; }; class LinkedList { private:...
Using java, create a class called MyString that has one String called word as its attribute...
Using java, create a class called MyString that has one String called word as its attribute and the following methods: Constructor that accepts a String argument and sets the attribute. Method permute that returns a permuted version of word. For this method, exchange random pairs of letters in the String. To get a good permutation, if the length of the String is n, then perform 2n swaps. Use this in an application called Jumble that prompts the user for a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT