-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_word.cpp
More file actions
69 lines (56 loc) · 1.22 KB
/
reverse_word.cpp
File metadata and controls
69 lines (56 loc) · 1.22 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* 题目:给定一个字符串,反转字符串中的单词,
*
* */
#include <stack>
#include <iostream>
using namespace std;
int is_letter(char c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
return 1;
} else {
return 0;
}
}
void reverse_word(char *in)
{
int start = 0;
int end = 0;
stack<char> my_stack;
while (1) {
if (is_letter(in[start])) {
end = start;
my_stack.push(in[end]);
end ++;
while (1) {
if (is_letter(in[end])) {
my_stack.push(in[end]);
end ++;
} else {
break;
}
}
while(!my_stack.empty()) {
in[start] = my_stack.top();
my_stack.pop();
start ++;
}
start ++;//move to next un-letter char
} else {
if (!in[start])
break;
start ++;
end ++;
}
}
}
int main()
{
char a[100] = "Hello, world!";
snprintf(a, 100, "Hello, world!");
a[99] = 0;
reverse_word(a);
printf("result: %s", a);
return 0L;
}