In this assignment you will implement a class called "string_class". Place the class declaration in the file "string_class.h" and the class implementation in the file "string_class.cpp". The class has the following characteristics"

1. A private string variable "current_string".

2. A default constructor that sets "current_string" to an empty string ("").

3. An explicit-value constructor that sets "current_string" equal to the argument that is passed to the explicit-value constructor when a string_class object is declared.

4. A public member Boolean function called "palindrome" that returns true if the current_string reads the same forward asit does backwards; otherwise it return false. For example "madam", "463364", and "ABLE WAS I ERE I SAW ELBA" are all palindromes.

5. A public member string function called "replace_all" that accepts two string arguments, " old_substring" and "new_substring". The function will return a copy of the string "current_string" with each occurrenceof "old_substring" replacedby "new_substring". For example, when the function is invoked, if current_string = "aaabbacceeaaa", old_substring = "aa", and new_substring= "zzz", then after execution of the function, current_string = "zzzabbacceezzza", and the function will return "zzzabbacceezzza". NOTE: DO NOT USE THE STRING CLASS FUNCTIONS "find", "replace", or "substr".

6. Overload the insertion operator (<<) as a friend function of the class with chaining to print the contents of a string_class object’s "current_string".

7. You make implement other class member functions if necessary

Call the driver to test the functionality of string_class, "stringclass_driver.cpp". See the sample main program below. You can use this to test your driver. The file is also posted in Canvas. You should submit the files "string_class.h", "string_class.cpp", and "stringclass_driver.cpp" together in a zip file.

Respuesta :

Answer:

string_class.h:

include <string>

using namespace std;

class string_class

{

private:

  string current_string;

public:

  string_class();

  string_class(string temp);

  bool palindrome();

  string replace_all(string Old, string New);

  friend ostream& operator<<(ostream& os,string_class& temp);

};

string_class.cpp:

#include "string_class.h"

#include<algorithm>

using namespace std;

string_class::string_class()

{

  this->current_string = "";

}

string_class::string_class(string temp)

{

  this->current_string = temp;

}

bool string_class::palindrome()

{

  string temp = this->current_string;

  reverse(temp.begin(), temp.end());

  if (temp == this->current_string)

      return true;

  return false;

}

string string_class::replace_all(string Old, string New)

{

  for (int i = 0; i < int(this->current_string.length()); i++)

  {

      string replace = "";

      for (int j = 0; j < int(Old.length()); j++)

          replace += this->current_string[i + j];

      if (replace == Old)

      {

          string final = "";

          for (int j = i + Old.length(); j < int(this->current_string.length()); j++)

              final += this->current_string[j];

          this->current_string = final;

      }

  }

  return this->current_string;

}

ostream& operator<<(ostream& co,string_class& temp)

{

  co << temp.current_string;

  return co;

}

test.cpp:

#include "string_class.h"

#include <iostream>

#include <string>

using namespace std;

int main()

{

  string_class s;

  cout << "**************" << endl

      << "Test#1: tesing default constructor and overloaded operator<< with chaining\n"

      << s << "1st blank line" << endl << s << "2nd blank line" << endl

      << "Test#1 Ended" << endl

      << "**************" << endl;

  string_class r("hello");

  cout << "**************" << endl

      << "Test#2: tesing explicit-value constructor and overloaded operator<< with chaining\n"

      << r << "1st blank line" << endl << r << "2nd blank line" << endl

      << "Test#2 Ended" << endl

      << "**************" << endl;

  cout << "**************" << endl

      << "Test#3: tesing palindrome\n"

      << "**************" << endl;

  string response = "Y";

  string ss;

  while (response == "Y" || response == "y")

  {

      cout << "Enter String: ";

      cin >> ss;

      string_class main_string(ss);

      if (main_string.palindrome())

      {

          cout << ss << " is a palindrome\n";

      }

      else

      {

          cout << ss << " is not a palindrome\n";

      }

      cout << "Would you like to try another string? (Y or N): ";

      cin >> response;

  }

  cout << "Test#3 Ended" << endl

      << "**************" << endl;

  cout << "**************" << endl

      << "Test#4: tesing replace_all\n"

      << "**************" << endl;

  response = "y";

  string current, old_substring, new_substring;

  while (response == "Y" || response == "y")

  {

      cout << "Enter value for current_string: ";

      cin >> current;

      string_class current_string(current);

      cout << "Enter old_substring: ";

      cin >> old_substring;

      cout << "Enter new_substring: ";

      cin >> new_substring;

      cout << "Current string = " << current << endl

          << "New string = " << current_string.replace_all(old_substring, new_substring)

          << endl;

      cout << "Would you like to try another string? (Y or N): ";

      cin >> response;

  }

  cout << "**************" << endl

      << "Test#4: tesing replace\n"

      << "**************" << endl;

  return 0;

}

Explanation:

  • Declare the methods inside the string_class.h.
  • In the string_class.cpp, create the palindrome method that checks the whether a string is palindrome by comparing it with its reverse version.
  • In the test.cpp file, test the program by calling the methods.