LeetCode 344. Reverse String

https://leetcode.com/problems/reverse-string/

Write a function that takes a string as input and returns the string reversed.

Example:
Given s = "hello", return "olleh".

转换字符串。string跟char之间的赋值又忘了怎么弄了。。。

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 class Solution {
 6 public:
 7     string reverseString(string s) {
 8         int i = 0, j = s.length() - 1;
 9         char* sTemp = (char*)s.c_str();
10         char temp;
11         
12         while (i < j)
13         {
14 //            swap(s[i++], s[j--]);
15             temp = sTemp[i];
16             sTemp[i] = sTemp[j];
17             sTemp[j] = temp;
18             i++;
19             j--;
20         }
21         
22         return (string)sTemp;
23     }
24 };
25 
26 int main ()
27 {
28     Solution testSolution;
29     string result = testSolution.reverseString("hello");    
30     
31     cout << result << endl;
32 
33     char ch;
34     cin >> ch;
35     
36     return 0;
37 }
View Code

swap (string)

http://www.cplusplus.com/reference/string/string/swap-free/

C++中string的用法 string字符串的使用方法
http://jingyan.baidu.com/article/20b68a8854f919796dec6265.html

标准C++中的string类的用法总结
http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html

c++中string的用法
http://www.cnblogs.com/yxnchinahlj/archive/2011/02/12/1952550.html

浅析string 与char* char[]之间的转换
http://www.jb51.net/article/41917.htm

LeetCode 344. Reverse String

原文地址:https://www.cnblogs.com/pegasus923/p/5496001.html