There are 2 issues here:
- the last assignment uses
input[index]
instead oftemp
on the right hand side - You iterate until the index reaches the end, which means every corresponding pair of indices is swapped twice resulting in the original string after fixing just (1.)
if (!input.empty())
{
for (size_t index = 0, index2 = input.size() - 1; index < index2; ++index, --index2)
{
char temp = input[index];
input[index] = input[index2];
input[index2] = temp;
}
}
1
solved Reversing string input not giving right output [closed]