Finding and Replacing elements in a string

Okay, I'm making identical programs in C++ and Java (just in terms of doing the same thing) and I'm making an encryption program. My Java one is almost done and I've just started the C++ one. Is there a way to check for certain letters in a string and then replace them in C++? I know there's a find function but it won't work the way I want it to. This is the set up for replacing a letter in a string in Java:

if (pdphrase.contains("a")) {

pdphrase = pdphrase.replaceAll("a", "P1");

}

I need to do the same thing but in C++ and I can't find the methods to do so. Am I missing them or are they just not there?

This is what I've done in C++ but I know it doesn't work.

if (pdphrase.find("a")) {

pdphrase.append("Z2");

}
Last edited on
Google is a marvellous thing. If you'd googled c++ string find and c++ string replace you'd have found two functions called, surprisingly, find and replace.
I used find and replace but replace won't work for what I want. With the replace method you have to specify the location in the string to change you can't just do pdphrase.replace("a", "Z2");. I need to have the program find all the instances of the letter and replace them without me putting in the exact location. I've already looked at both of those methods on this website and tried using them before using append.

http://www.cplusplus.com/reference/string/string/replace/
Last edited on
You can try using either a while loop or alternatively std::replace to mimic replaceAll. Surely those would work?

-Albatross

Try the Boost libraries; something like this, I think.

1
2
3
#include <boost/algorithm/string/replace.hpp>

boost::replace_all(inputString, "a", "Z2");

EDIT: I changed the code to this:
1
2
3
4
5
               for (int x = 0; x <= phraseLength; x++) {
                       
                       pdphrase.replace(x, x, "Z2");                      
                     
               }


It changes it to ZZ2 not just Z2. If you type in more that one a it will just put more Z's in front of it and then a 2.
Last edited on
Thanks so much! I used it and got this:
1
2
3
4
5
6
7
               size_t j = pdphrase.find_first_of('a');
               while(j != std::string::npos)
               {
                       pdphrase.replace(j, 1, "Z2");
                       j = pdphrase.find_first_of('a', j + 1);

               }


I took out the size_t for the other times I need to check for different letters because its obviously declared at the start and this works perfectly! Thanks a ton!
1
2
3
4
5
6
7
8
9
  std::string C = "aTbTcT";
  size_t j = C.find_first_of('T');
  while(j != std::string::npos)
  {
    C.replace(j, 1, "PHANTOM");
    j += strlen("PHANTOM") + 1; //this line is a must because "PHANTOM" contains "T"
    j = C.find_first_of('T', j);
  }
  cout<<C<<"\n";


the second parameter of replace means
how many times do you want me to replace it by the string "PHANTOM"?

ps : if you and your comrades don't deny to install the other lib, boost is always a good answer
Last edited on
Topic archived. No new replies allowed.