Something like this. I realize that my first for loop will not capture all of the characters. It misses the middle one. But run this, and you'll see what I mean that I cannot get the "olleH" way. Thank you very much!
Code:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
void swap (char *front, char *rear);
using namespace std;
int main()
{
string input, word;
char *front, *rear;
vector <string> string; // This will hold the individual words temporarily until they are replaced
cout << "Enter a sentence that you would like to reverse: ";
getline(cin, input);
// put the sentence into a stream
istringstream break_apart(input);
while (break_apart >> word)
{
if (word.size() % 2)
{
for (int i = 0; i < (word.size() / 2); i++)
{
front = &word.at(i);
rear = &word.at(word.size() - 1 - i);
swap(*front, *rear);
cout << *front << " " << *rear << " - ";
}
}
else
for (int i = 0; i < (word.size() / 2); i++)
{
front = &word.at(i);
rear = &word.at(word.size() - 1 - i);
swap(*front, *rear);
cout << *front << " " << *rear << " - ";
}
string.push_back(word);
}
return 0;
}
void swap (char *front, char *rear)
{
char temp = *front;
*front = *rear;
*rear = temp;
}