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

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...
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...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
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...
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)...
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 (The Account Class) Design a class named Account that contains (keep the...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator functions for id, balance, and annualInterestRate....
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class should include the following private data: name - the customer's name, a string. phone - the customer's phone number, a string. email - the customer's email address, a string. In addition, the class should include appropriate accessor and mutator functions to set and get each of these values. Have your program create one Customer objects and assign a name, phone number, and email address...
Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT