Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Programming Requires a class I have already written and will include at the bottom In this assignment you will use the MyString class that

C++ Programming

Requires a class I have already written and will include at the bottom

In this assignment you will use the MyString class that you wrote earlier in this course.

A palindrome is a string that reads the same forward and backward. Write a program that reads in any number of MyStrings from the user and determines if each MyString is a palindrome. The user will terminate each MyString by pressing the return (or enter) button. The user will indicate that there are no more MyStrings by entering "quit".

To do this you must write a recursive function named isAPalindrome that takes a single MyString argument and two arguments that are bounds on array indices. The function must examine the part of the argument between the two bounds (including the bounds) and return true if this part of the argument is a palindrome, false if it is not a palindrome. The function must be recursive and must not call any other functions except ispunct(), isspace(), and toupper() (described below).

Do not make any changes to the MyString class (except, if necessary, to correct errors in your MyString class). In particular, note that the isAPalindrome() function is not part of the MyString class. You will be submitting only your client file containing your main() function and your isAPalindrome() function. Do not submit your MyString class. Do not use the C++ string class.

Follow this sample output closely:

Enter a string: Able was I, ere I saw Elba Able was I, ere I saw Elba is a palindrome. Enter a string: peanut butter peanut butter is not a palindrome. Enter a string: quit 

In determining whether a string is a palindrome, this function must consider uppercase and lowercase letters to be the same and ignore punctuation characters and whitespace characters. You must not modify the argument in any way or make a copy of the argument to accomplish this. The simplest way to handle uppercase/lowercase issues is to make everything uppercase on the fly, right at the instant that you are comparing the two characters.

Hints:

  • You will want to use three functions that are in the cctype library (i.e. you must #include cctype to use them). They are ispunct(char), a bool function which returns true if its argument is a punctuation mark and false otherwise, isspace(char), which returns true if its argument is a whitespace character and false otherwise, and toupper(char), which returns the uppercase equivalent of its argument (without changing the argument itself).

  • I strongly suggest that you first write this program so that it works in the general case (i.e. assuming that all letters are uppercase and no characters are punctuation characters or whitespace characters), and then add code to handle the uppercase/lowercase and punctuation/whitespace issues.

  • Make sure you try a palindrome that starts and ends with the same letter but is not a palindrome. Returning a false positive in this case is one of the most common errors that students make.

  • Another common error is calling this value-returning function as if it was a void function. You can't say "isAPalindrome(str1, leftBound, highBound);" on a line by itself. It doesn't do anything.

  • Also, make sure that every case in your function has a return statement. Often students submit programs that worked perfectly for them, but they were just getting lucky, and the computer was returning the correct value by chance when there was no return statement.

The MyString Class

#ifndef MYSTRING_H #define MYSTRING_H #include  namespace compsci_mystring{ class MyString { public: MyString(const char* inString); MyString(); MyString(const MyString& copyMe); ~MyString(); friend std::ostream& operator<<(std::ostream& out, const MyString& printMe); friend std::istream& operator>>(std::istream& in, MyString& readMe); void read(std::istream& in, char delimeter); static const int MAX_INPUT_SIZE = 127; char operator[] (int index) const; char& operator[](int index); 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); MyString operator=(const MyString& right); friend MyString operator+(const MyString& left, const MyString& right); MyString operator+=(const MyString& right); int length() const; private: char *str; }; } #endif /* File: mystring.cpp CLASS INVARIANT: The class has one private data member defined as follows: char *str; str always represents a valid null-terminated c-string */ #include "mystring.h" #include  #include  #include  using namespace std; namespace compsci_mystring{ MyString::MyString(const char* inString) { str = new char[strlen(inString) + 1]; strcpy(str, inString); } MyString::MyString() { str = new char[1]; strcpy(str, ""); } MyString::MyString(const MyString& copyMe) { str = new char[strlen(copyMe.str) + 1]; strcpy(str, copyMe.str); } MyString::~MyString() { delete [] str; } ostream& operator<<(ostream& out, const MyString& printMe) { out << printMe.str; return out; } istream& operator>>(istream& in, MyString& readMe) { delete [] readMe.str; char tempStr[MyString::MAX_INPUT_SIZE + 1]; in >> tempStr; readMe.str = new char[strlen(tempStr) + 1]; strcpy(readMe.str, tempStr); return in; } void MyString::read(istream& in, char delimiter) { char tempStr[MyString::MAX_INPUT_SIZE + 1]; in.getline(tempStr, MyString::MAX_INPUT_SIZE + 1, delimiter); delete [] str; str = new char[strlen(tempStr) + 1]; strcpy(str, tempStr); } char MyString::operator[](int index) const { assert (index >= 0 && index < strlen(str)); return str[index]; } char& MyString::operator[](int index) { assert (index >= 0 && index < strlen(str)); return str[index]; } bool operator<(const MyString& left, const MyString& right) { return strcmp(left.str, right.str) < 0; } bool operator>(const MyString& left, const MyString& right) { return strcmp(left.str, right.str) > 0; } bool operator<=(const MyString& left, const MyString& right) { return strcmp(left.str, right.str) <= 0; } bool operator>=(const MyString& left, const MyString& right) { return strcmp(left.str, right.str) >= 0; } bool operator==(const MyString& left, const MyString& right) { return strcmp(left.str, right.str) == 0; } bool operator!=(const MyString& left, const MyString& right) { return strcmp(left.str, right.str) != 0; } MyString MyString::operator=(const MyString& right) { if (this !=& right){ delete [] str; str = new char[strlen(right.str) + 1]; strcpy(str, right.str); } return *this; } MyString operator+(const MyString& left, const MyString& right) { MyString tempStr; delete [] tempStr.str; tempStr.str = new char[strlen(left.str) + strlen(right.str) + 1]; strcpy(tempStr.str, left.str); strcat(tempStr.str, right.str); return tempStr; } MyString MyString::operator+=(const MyString& right) { *this = *this + right; return *this; } int MyString::length() const { return strlen(str); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

C++ Database Development

Authors: Al Stevens

1st Edition

1558283579, 978-1558283572

More Books

Students also viewed these Databases questions