fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int RemoveWordFromLine(string &line, const string& word)
  7. {
  8. int no_of_occurence=0;
  9. int const length_of_stopword=word.length();
  10.  
  11. for(int j=0 ;j<=line.length()-length_of_stopword;j++){
  12. if ( j+length_of_stopword<=line.length()){
  13. if ((j==0 || line[j-1]==' ') && (j+length_of_stopword==line.length()
  14. || line[j+length_of_stopword]==' ' ) ) {//I have to check this to ensure 'a' in "air" is not removed by the function.
  15. if(line.substr(j,length_of_stopword)==word){
  16. line.replace(j,length_of_stopword,"*");
  17. no_of_occurence++;
  18. }
  19. }
  20. }
  21. }
  22. }
  23.  
  24. int main() {
  25. vector<string> lines {
  26. "The house whirled around two or three times and rose slowly",
  27. "Then the house whirled around two or three times and rose slowly",
  28. "through the air. Dorothy felt as if she were going up in a balloon.",
  29. "The north and south winds met where the house stood, and made it the",
  30. "The north and south winds met where the house stood, and made it the ",
  31. "exact center of the cyclone."
  32. };
  33. vector<string> stopwords {
  34. "a",
  35. "an",
  36. "A",
  37. "and",
  38. "The",
  39. "the",
  40. "in",
  41. "or",
  42. "of"
  43. };
  44.  
  45. int i=0;
  46. for (auto& line : lines) {
  47. cout<<++i<<") "<<line<<endl;
  48. for (auto& word : stopwords) {
  49. RemoveWordFromLine(line,word);
  50. }
  51. cout<<" => "<<line<<endl;
  52. }
  53. return 0;
  54. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
1) The house whirled around two or three times and rose slowly
  => * house whirled around two * three times * rose slowly
2) Then the house whirled around two or three times and rose slowly
  => Then * house whirled around two * three times * rose slowly
3) through the air. Dorothy felt as if she were going up in a balloon.
  => through * air. Dorothy felt as if she were going up * * balloon.
4) The north and south winds met where the house stood, and made it the
  => * north * south winds met where * house stood, * made it *
5) The north and south winds met where the house stood, and made it the 
  => * north * south winds met where * house stood, * made it * 
6) exact center of the cyclone.
  => exact center * * cyclone.