0
How do I avoid errors when altering string that's under 15 characters long to greater than 15?
In my program I search a string for numbers and if it's a number between 1 and 10 it writes out the number. Like '1' is changed to 'one'. My code works if the final string is under 15 characters but if the original string was 14 characters or less and the output altered version is 15 or greater I get a time out error. https://code.sololearn.com/cGp7B1jZ7e7B/?ref=app
2 Réponses
+ 4
Internally, a string allocates a buffer of a fixed size, and when the content outgrows that buffer, a larger one is allocated elsewhere and the existing string is copied there. This process invalidates all existing iterators to the string, since they now point to a deleted memory location, and is what you observe once your string exceeds a certain size.
+ 2
I edited your code a bit.
https://www.sololearn.com/compiler-playground/cVCFDPqK5B3Q
This could help you.