-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12605.cpp
More file actions
72 lines (53 loc) · 1.32 KB
/
12605.cpp
File metadata and controls
72 lines (53 loc) · 1.32 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
69
70
71
72
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
using namespace std;
// 단어를 반대 순으로 출력한다.
void change_string(string *str, int blank) {
int word_count = blank + 1;
// 단어의 개수는 빈칸의 개수보다 하나 많다.
string* words = new string[word_count];
string tempstr = *str;
stringstream ss(tempstr);
ss.str(tempstr);
string word;
int i = 0;
while (ss >> word) {
words[i] = word;
i++;
}
string temp = "";
for (int i = word_count - 1; i >= 0; i--) {
temp += words[i] + " ";
}
*str = temp;
}
int main() {
int n;
cin >> n;
string* str = new string[n];
int* str_blank_count = new int[n];
cin.ignore();
// 반복문에서 getline 함수를 사용하려면 먼저 버퍼를 비워줘야 한다.
for (int i = 0; i < n; i++) {
getline(cin, str[i]);
str_blank_count[i] = 0;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < str[i].length(); j++)
if (str[i][j] == ' ')
str_blank_count[i]++;
}
// 공백의 수가 홀수 개라면 단어는 짝수 개
// 공백의 수가 짝수 개라면 단어는 홀수 개
// 공백의 수가 0개라면 단어는 1개
for (int i = 0; i < n; i++) {
change_string(&str[i], str_blank_count[i]);
}
// cout << "After Changed" << endl;
for (int i = 0; i < n; i++) {
cout << "Case #" << i + 1 << ": " << str[i] << endl;
// cout << "Case #" << i + 1 << ": " << str_blank_count[i] << endl;
}
}