How do I avoid errors when altering string that's under 15 characters long to greater than 15? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

9th Sep 2022, 6:35 AM
Malachite
Malachite - avatar
2 Answers
+ 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.
9th Sep 2022, 9:23 AM
Shadow
Shadow - avatar
+ 2
I edited your code a bit. https://www.sololearn.com/compiler-playground/cVCFDPqK5B3Q This could help you.
10th Sep 2022, 8:34 AM
SoloProg
SoloProg - avatar