-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy path句子逆序.cpp
60 lines (51 loc) · 818 Bytes
/
句子逆序.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
55
56
57
58
59
60
#include<iostream>
using namespace std;
void Reverse(char* pBegin, char* pEnd)
{
if (pBegin == NULL || pEnd == NULL)
return;
while (pBegin < pEnd)
{
char temp = *pBegin;
*pBegin = *pEnd;
*pEnd = temp;
pBegin++;
pEnd--;
}
}
char* ReverseSentence(char* str)
{
if (str == NULL) return NULL;
char* pBegin = str;
char *pEnd = str;
while (*pEnd != '\0') pEnd ++;
pEnd--;
//翻转整个句子
Reverse(pBegin, pEnd);
//翻转句子中每个单词
pBegin = pEnd = str;
while (*pBegin != '\0')
{
if (*pBegin == ' ')
{
pBegin ++;
pEnd ++;
}
else if (*pEnd == ' ' || *pEnd == '\0')
{
Reverse(pBegin, --pEnd);
pBegin = ++pEnd;
}
else
{
pEnd ++;
}
}
return str;
}
void main()
{
char str[1000];
cin.getline(str, 1000);
cout << ReverseSentence(str) << endl;
}