Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
1.直接调用c++ STL
reverse(s.begin(), s.end())
2.
1 class Solution { 2 public: 3 string reverseString(string s) { 4 int size = s.length(); 5 for(int i = 0, j = size - 1; i < j; i++, j--) 6 swap(s[i], s[j]); 7 return s; 8 } 9 };
1 char* reverseString(char* s) { 2 int i = 0, j = strlen(s) - 1, tmp; 3 for(; i < j; i++, j--){ 4 tmp = s[i]; 5 s[i] = s[j]; 6 s[j] = tmp; 7 } 8 return s; 9 10 }
原文:http://www.cnblogs.com/qinduanyinghua/p/6357745.html