6
I’m trying to reverse a string in C but the function is clearing 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
}
'Cause I wipe the string when I make strlen again from that string?
The Hello string has 5 characters, so the size variable will be 5.~ by changing the values in the code above:
- tmp = buffer[0] ('H character')
- buffer[i] = buffer[size - i] (the position where the character H was will now have the value of the character of the position [5 - 0], that is, position 5, which corresponds to the character 'o')
- buffer[size - i] = tmp (since tmp had the character value already stored in buffer[i], then the position of the'o' character will have the character value 'H')
Is this analysis correct? What is the problem?
Done. Thanks again
– DarkGV