Question

7. Referring to the same String class as question 5, provide a member function that will...

7. Referring to the same String class as question 5, provide a member function that will remove any character that is supplied to the function as a parameter.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Assuming we can these fields in String class

class String {
private:
    char *data;
    int size;
};

7)
void String::remove(char ch) {
    int count = 0;
    for(int i = 0; i < size; ++i) {
        if(data[i] == ch) {
            ++count;
        }
    }
    char *newData = new char[size-count];
    int index = 0;
    for(int i = 0; i < size; ++i) {
        if(data[i] != ch) {
            newData[index++] = data[i];
        }
    }
    delete[] data;
    data = newData;
    size -= count;
}
Add a comment
Know the answer?
Add Answer to:
7. Referring to the same String class as question 5, provide a member function that will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Write the definition of a class  ContestResult containing: An data member winner of type  string , initialized...

    C++ Write the definition of a class  ContestResult containing: An data member winner of type  string , initialized to the empty string . An data member secondPlace of type  string , initialized to the empty string . An data member thirdPlace of type  string , initialized to the empty string . A member function called setWinner that has one parameter , whose value it assigns to the data member winner. A member function called setSecondPlace that has one parameter , whose value it assigns...

  • (i) Create a class square with attribute (integer) length which defaults to 5. Provide member functions...

    (i) Create a class square with attribute (integer) length which defaults to 5. Provide member functions that calculate the perimeter (4 * length) and the area (length*length) of the triangle. Also provide set and get functions for the length attribute. The set function should verify that length is between 0.0 to 100. Your class should also provide a display function that draws the aquare using * character. For example, if length = 5, the display function should draw             *****...

  • c++ class and member function

    Write a full class definition for a class named ContestResult , and containing the following members: A data member winner of type string , initialized to the emptystring. A data member secondPlace of type string , initialized to the empty string. A data member thirdPlace of type string , initialized to the empty string. A memberfunction called setWinner that has one parameter, whose value it assigns to the data member winner

  • Suppose the base class and the derived class each have a member function with the same...

    Suppose the base class and the derived class each have a member function with the same signature. When you have a pointer to a base class object and call a function member through the pointer, discuss what determines which function is actually called—the base class member function or the derived-class function.

  • If the derived class overrides a public member function of the base class, then to specify...

    If the derived class overrides a public member function of the base class, then to specify a call to that public member function of the base class you use the name of the base class, followed by the ____ operator, followed by the function name with the appropriate parameter list. Question 16 options: 1) : 2) . 3) :: 4) $

  • Question 9 4 pts If the derived class class Doverrides a public member function functionName of...

    Question 9 4 pts If the derived class class Doverrides a public member function functionName of the base class class, then to specify a call to that public member function of the base class you use the statement class:: functionName(); class:: functionName(); classD. functionName(); classB. functionName(); > Question 11 4 pts In a function template, a generic data type starts with the key word name that stands for the data type. followed by a parameter template class T function Question...

  • C++ A palindrome is a string that reads the same backward as forward. For example, the words mom, dad, madam and radar are all palindromes. Write a class Pstring that is derived from the STL string cl...

    C++ A palindrome is a string that reads the same backward as forward. For example, the words mom, dad, madam and radar are all palindromes. Write a class Pstring that is derived from the STL string class. The Pstring class adds a member functionbool isPalindrome( )that determines whether the string is a palindrome. Include a constructor that takes an STL string object as parameter and passes it to the string base class constructor. Test your class by having a main...

  • Write a full class definition for a class named Player , and containing the following members: A data member name of t...

    Write a full class definition for a class named Player , and containing the following members: A data member name of type string . A data member score of type int . A member function called setName that accepts a parameter and assigns it to name . The function returns no value. A member function called setScore that accepts a parameter and assigns it to score . The function returns no value. A member function called getName that accepts no...

  • C++ A palindrome is a string that reads the same backward as forward. For example, the...

    C++ A palindrome is a string that reads the same backward as forward. For example, the words mom, dad, madam and radar are all palindromes. Write a class Pstring that is derived from the STL string class. The Pstring class adds a member functionbool isPalindrome( )that determines whether the string is a palindrome. Include a constructor that takes an STL string object as parameter and passes it to the string base class constructor. Test your class by having a main...

  • Q1)(20p)Write a static member function called removeLongestRepeatingSegment that takes a String a...

    Q1)(20p)Write a static member function called removeLongestRepeatingSegment that takes a String argument. The method will return a new String by removing the logest segment that contains consequtively repeating characters. public static void main(String []args){ String s=”1111222223333311111111444455552222”; String res= removeLongestRepeatingSegment(s); will return “11112222233333444455552222” } public static String removeLongestRepeatingSegment(String s){ --------------- Q2)15p)Given a list of objects stored (in ascending order) in a sorted linked list, implement member method which performs search as efficiently as possible for an object based on a key....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT