Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using pointers and avoiding unnecessary local variables, write a function, rmchr

ID: 3822350 • Letter: U

Question

Using pointers and avoiding unnecessary local variables, write a function, rmchr, that takes a string and a character as arguments, removing all occurrences of the character). rmchr should not leave holes in the string. What should rmchr return?

Print the string both before and after your call to rmchr to demonstrate that the original string was modified.

Run your program using the following strings and characters:

   string                        character

"abracadabra"                 'a'

"abracadabra"                 'b'

"abracadabra"                 'n'

   "aaaa"                          'a'

   "aaaa"                          'n'

Explanation / Answer

#include <iostream>

using namespace std;
void rmchr (string *s, char c){
while(s->find(c) != -1){
int index = s->find(c);
s->erase(index, 1);
}
}
int main()
{
string s = "abracadabra";
char c = 'a';
rmchr(&s, c);
cout<<"New string: "<<s<<endl;

return 0;
}

Output:

h-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                           

New string: brcdbr

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote