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.
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 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.
Step by step
Solved in 2 steps with 1 images