Question
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
Code
#include<iostream>
#include<string>
using namespace std;
class Solution {
public:
string reverseString(string s) {
char temp;//临时变量
for (int i = 0; i < s.length() / 2; i++) {
temp = s[i];
s[i] = s[s.length() - 1 - i];
s[s.length() - 1 - i] = temp;
}
return s;
}
};
int main() {
Solution so;
string s = { "hello" };
cout << so.reverseString(s) << endl;;
system("pause");
return 0;
}