string::erase(pos,len)
erases
len
characters starting at position
pos
. You are erasing i characters starting at the beginning.
Also, be careful with your
for
loop. If you erase a character, then next char in the string moves to the position previously occupied by the character erased. You need to check if THAT character needs to be erased, so you don't want to increment
i
in that case.
Since the size of the string changes through the loop, you can't pre-compute the size. You need to check against the current size each time.
Your
if
condition is a little odd too. Why do you care about the value of the next character? Isn't it sufficient to say that the current character is a '0'?
Finally, consider using a iterator instead and notice that
erase(iterator)
returns a new iterator. This is sort of overkill, but it will get you accustomed to the way you can remove items from an STL collection.
http://www.cplusplus.com/reference/string/basic_string/erase/