If a string is defined in a function, does it retain its value between calls ?
Something like this :
std::string myFunction( std::string input)
{
std::string output;
for ( int i=0; i < input.length(); i++ )
{
output[i] = input[i];
}
return output;
}
If the input string’s length is longer in the first call to the function than the length of the input in the second call, then the string returned in the second call still has last few(same as the difference in length) characters of the previous call intact.
In well-defined code it doesn’t, unless it’s declared
static.The main problem with your current implementation is the body of the loop:
Here, you’re assigning past the end of
output, which is undefined behaviour. Once you’re in the realm of undefined behaviour, anything can happen.