In: Computer Science
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:
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.
ANS:
#include<string>
#include<iostream>
using namespace std;
class simpleString
{
private:
int capacity; // maximum size
char* s; // pointer to a dynamic storage array
int length; // current length
public:
simpleString() // Default constructor
{
capacity = 10;
s = new char[capacity];
length = 0;
}
simpleString(int mVal) // Explicit value
constructor
{
capacity = mVal;
s = new char[mVal];
}
~simpleString()
{ delete[] s; } // Destructor Deleting dynamic
character array in the end of program
void readString() // Read a simple string
{
cout << "Enter the string:"
<< endl;
cin >> s;
while (s[length] != '\0')
{
length = length
+ 1;
}
}
void writeString() const // Display a simple
string
{
cout << "Displaying the
String" << endl << endl;
for (int i = 0; i<=length;
i++)
{
cout <<
s[i];
}
}
char at(int pos) const // Return character at
(pos)
{
if (pos < length && pos
< capacity)
{
return
s[pos];
}
}
int getLength() const // Return the string
length
{
cout << "Length:" <<
endl;
return length;
}
int getCapacity() const // Return the string
capacity
{
cout << "Capacity:" <<
endl;
return capacity;
}
void copyContents(char a[]) const // Copy the
contents into an array
{
cout << "Copying contents
into an array" << endl;
for (int i = 0; a[i] != '\0';
i++)
{
s[i] =
a[i];
}
}
void eraseToEnd(int p)
{
cout << "Erasing elements
from p to the end" << endl;
//entereing -1 means erasing and
reducing length
length = p;
for (int i = p; s[i] !=
'\0'; i++)
{
s[i] = -1;
}
}
int findSub(simpleString sub)
{
for (int i = 0; i <
(length - sub.length); i++)
{
int j = 0;
while (j <
sub.length && (sub.s[j] == s[i + j]))
{
j++;
}
if (j ==
sub.length)
{
return i;
}
}
return -1;
}
};
int main()
{
simpleString obj(20);
obj.readString();
cout <<"at 5th index "<<
obj.at(5)<<endl;
cout << "Length of string:" <<
obj.getLength() << endl;
obj.writeString();
obj.eraseToEnd(3);
obj.writeString();
}
Comment down for any queries
Please give a thumbs up if you are satisfied with answer
:)