Question

In: Computer Science

Write C++ program Consider the following SimpleString class: class simpleString {     public:          simpleString( );...

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.

Solutions

Expert Solution

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 :)


Related Solutions

********************C# C# C#******************** Part A: Create a project with a Program class and write the following...
********************C# C# C#******************** Part A: Create a project with a Program class and write the following two methods(headers provided) as described below: 1. A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range...
write program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
Please write this program in C++ Write a program that           - a user-defined class Cuboid...
Please write this program in C++ Write a program that           - a user-defined class Cuboid which has the following elements:                    - default constructor (has no parameters)                    - constructor that has 3 parameters (height, length, width)                    - data members                              - height, length, width                    - member functions                              - to set the value of each of the data members - 3 functions                              - to get the value of each of the data members - 3...
Consider the following class definition:                   public class Parent {               private
Consider the following class definition:                   public class Parent {               private int varA;               protected double varB;               public Parent(int a, double b){ varA = a; varB = b;               }               public int sum( ){                    return varA + varB;               } public String toString( ){                    return "" + varA + "   " + varB;               }         } Consider that you want to extend Parent to Child. Child will have a third int instance data varC....
Consider the following class definition:                   public class Parent {               private
Consider the following class definition:                   public class Parent {               private int varA;               protected double varB;               public Parent(int a, double b){ varA = a; varB = b;               }               public int sum( ){                    return varA + varB;               } public String toString( ){                    return "" + varA + "   " + varB;               }         } Consider that you want to extend Parent to Child. Child will have a third int instance data varC....
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static...
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static Scanner console = new Scanner(System.in); public static void main(String[] args) { double num1; double num2; System.out.print("Enter two integers: "); num1 = console.nextInt(); num2 = console.nextInt(); System.out.println(); if (num1 != 0 && num2 != 0) System.out.printf("%.2f\n", Math.sqrt(Math.abs(num1 + num2 + 0.0))); else if (num1 != 0) System.out.printf("%.2f\n", Math.floor(num1 + 0.0)); else if (num2 != 0) System.out.printf("%.2f\n",Math.ceil(num2 + 0.0)); else System.out.println(0); }} a. What is the...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static void main(String[] args) { String bottle1_label="Milk"; float bottle1_volume=250; float bottle1_capacity=500; bottle1_volume=addVolume(bottle1_label, bottle1_volume,bottle1_capacity,200); System.out.println("bottle label: " + bottle1_label + ", volume: " + bottle1_volume + ", capacity: " +bottle1_capacity); String bottle2_label="Water"; float bottle2_volume=100; float bottle2_capacity=250; bottle2_volume=addVolume(bottle2_label, bottle2_volume,bottle2_capacity,500); System.out.println("bottle label: " + bottle2_label + ", volume: " + bottle2_volume + ", capacity: " +bottle2_capacity); } public static float addVolume(String label, float bottleVolume, float capacity, float addVolume)...
Write a C++ program where class 1 and class 2 (which is a base class) should...
Write a C++ program where class 1 and class 2 (which is a base class) should have a derived class (class 4 and class 5). Each of the derived classes should include at least 1 variable, 2 functions (one will be showing the function overriding case, the second one will be showing the function overloading case), 2 constructors (with default values, with parameters). Then class 3 (composition) (to relate class 1 and class 2) should include 3 variables (first variable...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** *...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each symbol (e.g., the * reverse complement of "GTCA" is "TGAC"). * * @param dna a char array representing...
Write a C program that calculates a student grade in the C Programming Class. Ask the...
Write a C program that calculates a student grade in the C Programming Class. Ask the user to enter the grades for each one of the assignments completed in class: Quiz #1 - 25 points Quiz #2 - 50 points Quiz #3 - 30 points Project #1 - 100 points Project #2 - 100 points Final Test - 100 points The total of the quizzes count for a 30% of the total grade, the total of the projects counts for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT