Replace All pi Problem


This is my solution for replace all pi problem.
It is working perfectly fine for inputs like xpix, xxpix, xpighpilmpipi. But when there are two or more same character after pi it is not giving the correct solution. Why?

hi @tanyaa.sood

in line 10 s.erase(i, i+1);

this include only one character
instead of that write s.erase(i, 2);

code will run correctly

you can read documentation for more details
http://www.cplusplus.com/reference/string/string/erase/

first, last
Iterators specifying a range within the string] to be removed: [first,last). i.e., the range includes all the characters between first and last , including the character pointed by first but not the one pointed by last .

No, its still not giving the correct output. Consider the input as “xxpixx”. The output is “xx3.14” which is incorrect.

@tanyaa.sood

there are 3 way to use erase
you can read about it in doucmentation

sequence (1) string& erase (size_t pos = 0, size_t len = npos);
character (2) iterator erase (iterator p);
range (3) iterator erase (iterator first, iterator last);

you want to use last one but you are using first one
to use 3rd one you have to provide iterators

simplest way is to use 1st one
in which you have to provide starting index and length of string to be erase

for this quesiton
correct way is : erase(i,2);
starting from i till 2 characters

Modified Code