-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqueeze.cpp
64 lines (58 loc) · 1.63 KB
/
squeeze.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
61
62
63
64
/******************************************************************************
* Compilation: g++ -o squeeze main.cpp -std=c++0x -g
* Execution: ./squeeze < input.txt
* Dependencies: std::in cout fstream
*
* Takes a string command line argument and removes adjacent spaces.
*
* $ more squeeze.data
* this is a test
* Hello world !
* Hello world !
*
* $ ./queeze < squeeze.data
* this is a test
*
******************************************************************************/
#include <iostream>
#include <fstream>
#include <string>
using std::string;
void squeeze(string* s) {
auto d_it = s->begin() + 1;
for (auto s_it=s->begin()+1; s_it!=s->end(); ++s_it) {
*d_it = *s_it;
if (*d_it != '\n') {
if (*d_it != ' ')
++d_it;
else if (*(d_it-1) != ' ')
++d_it;
} else {
if (*(d_it-1) == ' ') {
*(d_it-1) = *d_it;
}
}
}
s->resize(d_it - s->begin() + 1);
}
int main(int argc, char* argv[]) {
if (argc < 1) {
std::cout << "argument to main is insufficient!" << std::endl;
return -1;
}
string s, p;
while (std::getline(std::cin, p, '\n')) {
s.append(p).append("\n");
}
std::cout << "the origin string is:\n" << s << std::endl;
squeeze(&s);
std::cout << "after squeeze, the string is:\n" << s << std::endl;
std::ofstream out("result.txt");
out << s;
out.close();
return 0;
}
/******************************************************************************
* Copyright © 2000–2017, Jerick26.
* Last updated: Fri May 12 EST 2017.
******************************************************************************/