Write C++ program Consider the following SimpleString class:   class simpleString {     public:          simpleString( );                                 // Default constructor          simpleString(int mVal );                    // Explicit value constructor          ~simpleString() { delete [ ] s;}          // Destructor void readString();                              // Read a simple string          void writeString() const;                    // Display a simple string char at(int pos) const;                       // Return character at (pos)          int getLength() const;                        // Return the string length          int getCapacity() const;                     // Return the string capacity          void copyContents(char[ ]) const;     // Copy the contents into an array      private:          int capacity;                                      // maximum size            char *s;                                             // pointer to a dynamic storage array          int length;                                         // current length };   Add to the above class the following two public member function: A function eraseToEnd (p) to erase all characters starting from position (p) in the string to the end of the string. A function findsub (SimpleString sub). The function should return the position of start of substring (sub) in the string. If (sub) does not exist in the string, the function returns -1 Hint: Consider the problem of searching in a string of text T[0 .. n-1] for a substring that matches a pattern P[0..m-1]. A simple Brute Force algorithm is:   ALGORITHM StringMatch (T[0..n-1], P[0..m-1]) {       for i = 0 to n-m       {          j = 0;          while ( (j < m) AND (P[j] == T[i + j]) ) j++;          if ( j == m) return i;        }             return -1; }   Implement the function findsub (SimpleString sub) using the above algorithm.   Provide the implementation of the above two functions.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write C++ program

Consider the following SimpleString class:

 

class simpleString

{

    public:

         simpleString( );                                 // Default constructor

         simpleString(int mVal );                    // Explicit value constructor

         ~simpleString() { delete [ ] s;}          // Destructor

void readString();                              // Read a simple string

         void writeString() const;                    // Display a simple string

char at(int pos) const;                       // Return character at (pos)

         int getLength() const;                        // Return the string length

         int getCapacity() const;                     // Return the string capacity

         void copyContents(char[ ]) const;     // Copy the contents into an array

 

   private:

         int capacity;                                      // maximum size

           char *s;                                             // pointer to a dynamic storage array

         int length;                                         // current length

};

 

Add to the above class the following two public member function:

  1. A function eraseToEnd (p) to erase all characters starting from position (p) in the string to the end of the string.
  2. A function findsub (SimpleString sub). The function should return the position of start of substring (sub) in the string. If (sub) does not exist in the string, the function returns -1

Hint:

Consider the problem of searching in a string of text T[0 .. n-1] for a substring that matches a pattern P[0..m-1]. A simple Brute Force algorithm is:

 

ALGORITHM StringMatch (T[0..n-1], P[0..m-1])

{

      for i = 0 to n-m

      {

         j = 0;

         while ( (j < m) AND (P[j] == T[i + j]) ) j++;

         if ( j == m) return i;

       }

            return -1;

}

 

Implement the function findsub (SimpleString sub) using the above algorithm.

 

Provide the implementation of the above two functions.

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Data members
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education