[Solved] Reversing string input not giving right output [closed]


There are 2 issues here:

  1. the last assignment uses input[index] instead of temp on the right hand side
  2. 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]