-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_string.cpp
More file actions
52 lines (49 loc) · 1.2 KB
/
reverse_string.cpp
File metadata and controls
52 lines (49 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* =====================================================================================
*
* Filename: reverse_string.cpp
*
* Description: 344. Reverse String. Write a function that reverses a string. The
* input string is given as an array of characters char[].
*
* Version: 1.0
* Created: 04/08/19 12:10:15
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <string>
class Solution
{
public:
void reverseString(std::vector<char>& s)
{
int i = 0;
int j = s.size() - 1;
while (i < j)
{
std::swap(s[i++], s[j--]);
}
}
};
int main(int argc, char* argv[])
{
std::string s = "hello";
if (argc > 1)
{
s = argv[1];
}
std::vector<char> r(s.begin(), s.end());
Solution().reverseString(r);
printf("Input: `%s`\n", s.c_str());
printf("Reverse: `%s`\n", std::string(r.begin(), r.end()).c_str());
return 0;
}