运行结果:
Enter a string: The new operator takes a type for its argument.
.tnemugra sti rof epyt a sekat rotarepo wen ehT
reverseString.cpp
#include <iostream>
#include <string>
using namespace std;
void reverseString(string& s);
int main() {
string s;
cout << "Enter a string: ";
getline(cin, s);
reverseString(s);
cout << s << '\n';
return 0;
}
void reverseString(string& s) {
for (char *front = &s[0], *rear = &s[s.length() - 1], tmp; front < rear; front++, rear--) {
tmp = *front;
*front = *rear;
*rear = tmp;
}
}