-1

Trying to reverse swap all the characters in a string array, however, it's not outputting right. Does anyone know if I'm heading the right direction?

My code:

#include <string>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <cstring> // for strlen()
using namespace std;

void doSwap (char &string1, char &string2) {
    char temp;
    temp = string1;
    string1 = string2;
    string2 = temp;
}

int main() {
    string testingWord = "hello";
    int i;

    cout << testingWord << "\n";

    cout << "\tBelow is testing the swap feature:\n";

    for (i = 0; i < testingWord.size() - 1; i++) {
        doSwap(testingWord[i], testingWord[i+1]);
    }

    cout << testingWord << "\n";
}

Here is my output:

elloh

Edit: Not trying to do XOR swapping?

1 Answers1

0

Try this:

for (i = 0; i < testingWord.size() / 2; i++) {
    doSwap(testingWord[i], testingWord[testingWord.size() - i]);
}
theharls
  • 163
  • 7
  • *Brevity is acceptable, but fuller explanations are better.* - [How do I write a good answer?](http://stackoverflow.com/help/how-to-answer) – Jonny Henly May 03 '16 at 02:30