how do i erase letters from string from the position of the previously erased letter? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 26

how do i erase letters from string from the position of the previously erased letter?

if the end of the string is reached, the index restarts at 0. the last character that stands the process should be retained

12th Feb 2017, 1:38 AM
Jobelle
Jobelle - avatar
3 Answers
+ 3
u can erase letters with the erase() function takes 2 parameters, first one where to start and second one how many letters to erase, std::string hey = "hey"; hey.erase(0, 1); // now variable hey is equal to "ey"
12th Feb 2017, 1:44 AM
Kawaii
Kawaii - avatar
+ 24
here's my code. it doesn't work. #include <iostream> using namespace std; int main() { string str="FLAMES"; int x=1, pos=0, limit=10; while(str.size()>1){ if(x==limit){ str.erase(pos,1); x=1; } if(pos>=str.size()){ pos=0; } x++; pos++; cout<<str<<endl; } return 0; }
12th Feb 2017, 2:06 AM
Jobelle
Jobelle - avatar
+ 1
what errors it gives u
12th Feb 2017, 4:29 AM
Kawaii
Kawaii - avatar