-2

How i can Find and replace (Match Whole word). I have this.

  void ReplaceString(std::string &subject, const std::string& search, const std::string& replace)
   {
    size_t pos = 0;
    while ((pos = subject.find(search, pos)) != std::string::npos) {
        subject.replace(pos, search.length(), replace);
        pos += replace.length();
    }
}

but it dosnt search for whole word. for example if i try

string test = "i like cake";
ReplaceString(test, "cak", "notcake");

it will still replace but i want it to match whole word.

Vada Poché
  • 763
  • 5
  • 16
  • 4
    Really? You couldn't search first? There have been at three related questions posted today. I highly suggest talking with your classmates. – Thomas Matthews Nov 22 '16 at 21:32
  • 2
    Possible duplicate of [Replace part of a string with another string](http://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string) – Hcorg Nov 22 '16 at 21:50
  • @Hcorg looking at that, it looks like this author either wrote the exact same function, or already tried that one (as the two samples are functionally identical, with only differing parameter names). – lcs Nov 22 '16 at 21:53
  • You can try to use regular expressions (i.e. the library in c++11 and beyond). [This page](http://www.cplusplus.com/reference/regex/regex_replace/) might be useful to get you going. – Jvinniec Nov 22 '16 at 22:05
  • Possible duplicate of [How can I implement an efficient whole-word string replacement in C++ without regular expressions?](http://stackoverflow.com/questions/5941908/how-can-i-implement-an-efficient-whole-word-string-replacement-in-c-without-re) – Paul Roub Nov 22 '16 at 22:24

2 Answers2

1

You're just blindly replacing any instances of search with replace without checking if they're full words prior to performing the replacement.

Here are just a couple of things you can try to work around that:

  • Split the string into individual words, then check each word against search, and replace if necessary. Then rebuild the string.
  • Replace only if pos-1 and pos + search.length() + 1 are both spaces.
lcs
  • 4,227
  • 17
  • 36
-1

Regular expressions solution if you have access to c++11 compiler:

#include <iostream>
#include <string>
#include <regex>

void ReplaceString(std::string &subject, const std::string& search, const std::string& replace)
{
    // Regular expression to match words beginning with 'search'
    std::regex e ("(\\b("+search+"))([^,. ]*)");
    subject = std::regex_replace(subject,e,replace) ;
}

int main ()
{
  // String to search within and do replacement
  std::string s ("Cakemoney, cak, cake, thecakeisalie, cake.\n");
  // String you want to find and replace
  std::string find ("cak") ;
  // String you want to replace with
  std::string replace("notcake") ;

  ReplaceString(s, find, replace) ;

  std::cout << s << std::endl;

  return 0 ;
}

Output: Cakemoney, notcake, notcake, thecakeisalie, notcake.

More about the regular expression string (\\b("+search+"))([^,. ]*). Note that after replacing search this string will be: (\\b(cak))([^,. ]*)

  • \b(cak) - match words beginning with cak regardless of what comes after
  • ([^,. ]*) - matches anything up to a ,, ., or (space).

The above basically just rips off the example provided here. The answer is case sensitive, and will also replace punctuation other than the three listed after ^, but feel free to learn more about regular expressions to make a more general solution.

Jvinniec
  • 616
  • 12
  • 18