I am trying to erase a character from a string. I have tried the code below:
size_t it = s.find(char(i+97)); //dont mind the i, it is just the int in a for loop.
s.erase(it);
Here is a test case:
Input: "cccaabababaccbc"
Output: "ccc"
Any ideas on why this is happening?
When you pass in an index, to erase, it goes till the end of the string. Change it to s.erase(it,1). The second parameter indicates the number of characters to remove.
I suggest not naming the variable it, that name is typically used for iterator types.
If you pass an iterator to erase, then only that character is removed.
Heres the docs: https://www.cplusplus.com/reference/string/string/erase/