I want to replace all occurrences of '$'
with "$$"
. Currently I am using string::find
to check if '$'
is present in string or not. then I am using for loop and checking every character if it matches with '$'
. If a character matches with $
then I am using string::replace
to replace it.
Is there any other effective method to do this in c++? without traversing entire string with less complexity?
Asked
Active
Viewed 395 times
0

alohamora
- 61
- 5
-
1@George exactly! here, there is no need to find the occurrences and then replace them. You could rather just use `std::replace` to achieve what you want to. – Zac Nov 08 '17 at 07:44
-
3I doubt that it's possible to do this without traversing the entire string. How are you going to check every character otherwise? If you call some function to do it, that function will effectively do the traversal for you. – Carlos Nov 08 '17 at 07:48
-
Neha: could you [edit] your question to explain why it is not a duplicate of linked thread? – YSC Nov 08 '17 at 07:50
-
1@Robin: Isn't `std::replace` for replacing one character with another one, but not for replacing a single character with a string like `"$$"`? – Stephan Lechner Nov 08 '17 at 08:03
-
My bad! Seems like same problem is described in that [question](https://stackoverflow.com/questions/14113173/replace-the-character-with-two-other), which deals with looping over entire string. – Robin Nov 08 '17 at 08:09
-
3dup https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string – sp2danny Nov 08 '17 at 08:10
-
What's wrong with your existing code? Please show it. – Jabberwocky Nov 08 '17 at 08:11
-
Stephan Lechner has posted an answer. I had used exactly same logic in my code. But I am searching if there is any better way to do this without traversing entire string? – alohamora Nov 08 '17 at 08:57
-
to replace one character of a string with other character we use `std::replace`. similarly is there any built in algorithm to replace occurrences of a character with string? Or can we do it without traversing entire string? – alohamora Nov 08 '17 at 08:59
1 Answers
0
I don't know a standard function which replaces ALL occurrences of a certain string with another string. The replace
-functions either replace all occurrences of a specific character by another character, or replace a single range of characters with another range.
So I think you cannot avoid iterating through the string on your own.
In your specific case, it might be easier, as you just have to insert an additional $
for every $
-character you find. Note the special measure avoiding an endless loop, which would happen if one doubled also the $
-value just inserted again and again:
int main() {
string s = "this $ should be doubled (i.e. $), then. But $$ has been doubled, too.";
auto it = s.begin();
while (it != s.end()) {
if (*it == '$') {
it = s.insert(it, '$');
it += 2;
}
else {
it++;
}
}
cout << s;
}

Stephan Lechner
- 34,891
- 4
- 35
- 58
-
yeah, I did exactly same. But here we are checking all characters, right? I am asking that instead of traversing entire string , is it possible to do this? – alohamora Nov 08 '17 at 08:54
-
you could use `std::find`, of course, which gives you the next occurrence of a character. But anyway, even `std::find` will traverse the string and check each character. I'm quite sure that this doesn't make things clearer or in another way better... – Stephan Lechner Nov 08 '17 at 09:05