/*Problem: Write a function CensorString that takes two strings as input and returns the first string
with all of the characters that are present in the second removed.*/
#include <iostream>
#include <string>
using namespace std;
string CensorString(string text, string remove);
string CensorCharacter(string text, char remove);
int main(){
string a = "the lazy fox jumped over the fence";
string b = "abc";
string c = CensorString(a, B)/>;
cout << c << endl;
}
string CensorString(string text, string remove){
string result;
for (int i = 0; i < remove.length(); i++){
result = CensorCharacter(text, remove[i]);
}
return result;
}
string CensorCharacter(string text, char remove){
string result;
for (int i = 0; i < text.length(); i++){
if (text[i] != remove)
result += text[i];
}
return result;
}
The program only removes the last character of String b from String a. The issue I believe is in lines 21-22.
I was hoping the string result would take the return string of the method CensorCharacter. In the next pass of the loop, the changed string result would then run through CensorCharacter again taking out the next character and continue. Hoping I conveyed my through process well enough to understand what I am trying to do.
Any ideas how to fix 21-22 so it will run the revised string through each pass of the for loop? I have no idea how to do it. Also, any idea if 21-22 would have worked as I intended in Java?
Thanks!

New Topic/Question
Reply




MultiQuote





|