-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverseSentence.cpp
58 lines (46 loc) · 1.07 KB
/
reverseSentence.cpp
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
//
// Created by Pasco on 16/6/17.
//
#include "catch.hpp"
#include <iostream>
using namespace std;
class Solution {
public:
void reverseSentence(char *str)
{
int size = 0;
int wbegin = 0;
while(str[size] != '\0')
{
if (str[size] == ' ')
{
reverseWord(str, wbegin, size-1);
wbegin = size+1;
}
size++;
}
reverseWord(str,wbegin,size-1);
for (int i = 0; i <= size; i++,size--) {
swap(str[i], str[size-1]);
}
// cout<<str<<endl;
}
void reverseWord(char *str, int begin, int end)
{
if(begin >= end)
return;
while (begin < end)
{
swap(str[begin], str[end]);
begin++;
end--;
}
}
};
TEST_CASE("111reverseSentence"){
Solution solution;
char str[] = "i love study, study make me happy";
char str1[] = "happy me make study study, love i";
solution.reverseSentence(str);
REQUIRE(strcmp(str,str1)==0);
}