2

I want to replace character 'ü' in a string if found in it. My code replaces ü, but also deleting other letters in the string.

 if (word.find('ü') != string::npos) 
 {
     word.replace(word.find('ü'), 'ü', "ue");
 }
JeJo
  • 30,635
  • 6
  • 49
  • 88
  • 1
    Note that [this question](https://stackoverflow.com/questions/10030626/replace-char-in-string-with-some-string-inplace) asks something similar. – KorbenDose Jun 12 '18 at 08:00
  • `'ü'` as character constant will only work with single byte encoding (i.e. _not_ UTF-8). You should use the string constant `"ü"` instead. This will also work with UTF-8. – Adrian W Jun 12 '18 at 08:14

3 Answers3

2

Not a one-liner, but erase followed by insert is clear enough.

size_t x = word.find('ü');
while (x != string::npos)
    {
    word.erase(x, 1);
    word.insert(x, "ue");
    x = word.find('ü');
    }
acraig5075
  • 10,588
  • 3
  • 31
  • 50
1

You can find the position of ü, starting from index 0 until end of the string and whenever you find, replace it with ue using the information of both position where you found and the length of the string you would like to find in the given string.

Something like follows: SEE LIVE HERE

#include <iostream>
#include <string>
#include <algorithm>
#include <cstddef>

int main()
{
   std::string word("Herr Müller ist ein König.");

   std::string findThis = "ü";
   std::string replaceWith = "ue";

   std::size_t pos = 0;
   while ((pos = word.find(findThis, pos)) != std::string::npos)
   {
      word.replace(pos, findThis.length(), replaceWith);
      pos += replaceWith.length();
   }

   std::cout << word << std::endl;
   return 0;
}

Output:

Herr Mueller ist ein König.
JeJo
  • 30,635
  • 6
  • 49
  • 88
  • What is a Kuenig? :) – Goswin von Brederlow Jun 12 '18 at 07:41
  • @GoswinvonBrederlow 'König' is the german word for king. – izlin Jun 12 '18 at 07:43
  • 1
    If it was written with "ö", it would have meant king in German :) But anyway, thank you JeJo! – quitely_zealous Jun 12 '18 at 07:45
  • I know that König is king. I was commenting on the fact that he replaced ö with ue. Fixed now in the answer. – Goswin von Brederlow Jun 12 '18 at 07:49
  • This would be O(n^2) in general? Though in this particular case it doesn't change the length in bytes of the string because ASCII characters only take one byte in UTF-8 (see https://www.ideone.com/HnRErj). A marginally smart .replace() should be able to ensure O(n) in this case, e.g. https://stackoverflow.com/questions/14321174/is-stdstringreplace-optimized-for-same-length-strings – gmatht Jun 12 '18 at 07:53
  • I am not sure what you mean by your solution is a generalised case. With n as the combined length of the input and output strings, in the general case your solution provides O(n^2) performance. It would be possible to get O(n) performance in the general case, I presume boost::replace_all does this. – gmatht Jun 12 '18 at 16:31
  • `find()` should take a total of O(n) time, but `replace()` presumably takes O(n) time if it has to move the remainder of the string, and you call it up to n times, so O(n*n) time in total. – gmatht Jun 12 '18 at 17:15
1

If using boost is an option you can do something like this

#include <boost/algorithm/string.hpp>

int main()
{
    std::string str("Herr Müller ist ein König.");
    boost::replace_all(str, "ü", "ue");
    std::cout << str << std::endl;
    return 0
}
Yakir E
  • 84
  • 4