I'm trying to invert a string into C but the function is cleaning it:
for(i = 0; i < (int)(size/2); i++)
{
tmp = buffer[i]; //armazena o character inicial
buffer[i] = buffer[size - i]; //Troca o character da ponta oposta
buffer[size - i] = tmp; //Armazena o character inicial no buffer
}
Why do you clean the string when I re-strlen this string?
The Hello string has 5 characters, so the size variable will have the value 5. ~ changing the values in the above code:
- tmp = buffer [0] (or the 'H' character)
- buffer [i] = buffer [size - i] (the position where the character H was now has the character value of the position [5 - 0], ie the position 5, which corresponds to the character 'o' )
- buffer [size - i] = tmp (as tmp had the value of the character already buffered [i], then the position of character 'o' will have the value of character 'H')
Is this analysis correct? What's the problem?