-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseSentenceInplace.cpp
More file actions
57 lines (41 loc) · 883 Bytes
/
ReverseSentenceInplace.cpp
File metadata and controls
57 lines (41 loc) · 883 Bytes
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
53
54
55
56
57
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
void m_reverse(string &s, const int &left, const int &right) {
//reverse(s.begin()+left, s.begin()+right);
cout << left << " : " << right << endl;
int tmp = left;
while( tmp - left < (right - left)/2 ) {
char c = s[tmp];
s[tmp] = s[right-1-tmp+left];
s[right-1-tmp+left] = c;
tmp++;
}
}
int main(void) {
string s = "I am a student.";
int left = 0;
int right= s.length();
m_reverse(s, left, right);
int idx = 0;
while( left != s.length() ) {
if( idx == s.length() || s[idx] == ' ' ) {
right = idx;
if( s[left] != ' ' ) {
m_reverse(s, left, right);
}
left = idx;
idx++;
continue;
}
if( s[left] == ' ') {
left = idx;
}else {
right = idx;
}
++idx;
}
cout << s << endl;
return 0;
}