-6

I am trying to remove a specific character from a string but am having difficulties.

Ive tried using replace() and replacing the character with nothing, but the compiler complains about that.

string s = "Hello, this is a test";
replace (s.begin(), s.end(), 'l', '');
cout << s;

What i would like is for it to find and remove the character 'l' so it outputs "Heo, this is a test". Unfortunately i don't think replace() is the correct thing to use and am a bit stumped. Have only been learning programming for a couple weeks, so I'm sorry if this is a dumb question. Thanks :)

  • See this [link](http://www.cplusplus.com/reference/string/string/replace/). It should be `s.replace` instead of `replace` – GAVD Aug 02 '16 at 09:42
  • 2
    Can you show us the error, that you are getting? I am **sure** that it tells **exactly** what's wrong. @sinsuren No, it won't, since `''` is not a valid character. – Algirdas Preidžius Aug 02 '16 at 09:42
  • 5
    Possible duplicate of [How to remove all the occurrences of a char in c++ string](http://stackoverflow.com/questions/20326356/how-to-remove-all-the-occurrences-of-a-char-in-c-string) – t.niese Aug 02 '16 at 09:44
  • 2
    @sinsuren, @GAVD `std::string::replace` doesn't search characters and doesn't replace it with another. It removes one range and inserts another one. – ilotXXI Aug 02 '16 at 09:45
  • Are you search answer first you ask here? It's very simply question. – Elidor Aug 02 '16 at 12:04

2 Answers2

0

enter image description here

This is pretty staight foward. If you want to replace Hello with Heo call the function with these parameters ReplaceWord(str, "Hello", "Heo").

Please note that this example is case sensitive so if you use a lowercase h, it will not replace at all, it wont find the word, it has to be a uppercase H.

#include <iostream>
#include <string>
using namespace std;


void ReplaceWord( std::string& source, const char* WordToFind, const char* WordToReplace );

//program entry point
int main (){

    cout<<""<<endl;

    string str = "Hello, this is a test";

    cout<<"Before replace : "<<endl;
    cout<<str<<endl;
    cout<<""<<endl;

    ReplaceWord(str, "Hello", "Heo");

    cout<<"After replace : "<<endl;
    cout<<str<<endl;

    cout<<""<<endl;

return 0;
}


void ReplaceWord( std::string& source, const char* WordToFind, const char* WordToReplace ){

   size_t LengthOfWordToReplace = strlen(WordToFind);
   size_t replaceLen = strlen(WordToReplace);
   size_t positionToSearchAt = 0;

   //search for the next word 
   while ((positionToSearchAt = source.find(WordToFind, positionToSearchAt)) != std::string::npos)
   {
      //replace the found word with the new word
      source.replace( positionToSearchAt, LengthOfWordToReplace, WordToReplace );

      // move to next position
      positionToSearchAt += replaceLen; 
   }
}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
-3

Hoping that you have added #include<algorithm>, the error message should complain about the empty character constant. You have to use '\0' for null character.

std::replace (s.begin(), s.end(), 'l', '\0'); 

Since you are new to programming, I would tell you that it is better(a good practice) to use the namespace name along with the function rather than having using namespace std in your code.

Considering the comment from @mindriot fro this answer, I prefer to give another solution:

 s.erase( std::remove( s.begin(), s.end(), 'l' ), s.end() ) ;

You can also have a look at here for other related options.

Community
  • 1
  • 1
Jackzz
  • 1,417
  • 4
  • 24
  • 53
  • Not the downvoter, but I agree with the downvote: Your answer does not remove all occurrences of `'1'`, but _replaces_ them with a `'\0'`. That's not the same. It turns `"123123"` into `"\00023\00023"`, which is not the same as the desired `"2323"`. Also, adding NUL characters may break if there is other code expecting a NUL-terminated string. E.g., try sending it to `std::cout`. – mindriot Aug 02 '16 at 12:18
  • @mindriot: I agree with your second point. But nothing like that is mentioned in question. – Jackzz Aug 03 '16 at 06:24
  • @mindriot so what does the null character do then?? – Yunfei Chen Jul 02 '20 at 23:50