How to delete all occurrences of FOO in a string s = FOOTHEFOOFOOBEST for example so the output would be THE BEST without any extra spaces unlike my code does.
Here is an sample of my try:
#include <iostream>
#include <string>
int main() {
std::string s = "FOOTHEFOOFOOBEST";
for (int i = 0; i < s.size(); i++){
if (s[i] == 'F'&&s[i + 1] == 'O' && s[i + 2] == 'O'){
std::cout << ' ';
i += 2;
}
else
std::cout << s[i];
}
return 0;
}
The output of my code is THE BEST there's an extra space however I expected THE BEST, Is there a simpler way to do that?